public DataTable ShowName(DProduct product) { DataTable DtResult = new DataTable("product"); SqlConnection SqlCon = new SqlConnection(); try { SqlCon.ConnectionString = Conection.cn; SqlCommand SqlCmd = new SqlCommand(); SqlCmd.Connection = SqlCon; SqlCmd.CommandText = "spsearch_product"; SqlCmd.CommandType = CommandType.StoredProcedure; SqlParameter ParSearch = new SqlParameter(); ParSearch.ParameterName = "@textsearch"; ParSearch.SqlDbType = SqlDbType.VarChar; ParSearch.Size = 50; ParSearch.Value = product.TextSearch1; SqlCmd.Parameters.Add(ParSearch); SqlDataAdapter SqlData = new SqlDataAdapter(SqlCmd); SqlData.Fill(DtResult); } catch (Exception ex) { DtResult = null; } return(DtResult); }
public string Delete(DProduct product) { string resp = ""; SqlConnection SqlCon = new SqlConnection(); try { SqlCon.ConnectionString = Conection.cn; SqlCon.Open(); SqlCommand SqlCmd = new SqlCommand(); SqlCmd.Connection = SqlCon; SqlCmd.CommandText = "spdelete_product"; SqlCmd.CommandType = CommandType.StoredProcedure; //Initialize variables SqlParameter ParId = new SqlParameter(); ParId.ParameterName = "@id"; //same name as in the procedure ParId.SqlDbType = SqlDbType.Int; ParId.Value = product.Id; SqlCmd.Parameters.Add(ParId); //Execute the command resp = SqlCmd.ExecuteNonQuery() == 1 ? "OK" : "Not deleted"; } catch (Exception ex) { resp = "Something went wrong: " + ex.Message; } finally { if (SqlCon.State == ConnectionState.Open) { SqlCon.Close(); } } return(resp); }
public string Insert(DProduct product) { string resp = ""; SqlConnection SqlCon = new SqlConnection(); try { SqlCon.ConnectionString = Conection.cn; SqlCon.Open(); SqlCommand SqlCmd = new SqlCommand(); SqlCmd.Connection = SqlCon; SqlCmd.CommandText = "spinsert_product"; SqlCmd.CommandType = CommandType.StoredProcedure; //Initialize variables SqlParameter ParId = new SqlParameter(); ParId.ParameterName = "@id"; ParId.SqlDbType = SqlDbType.Int; ParId.Direction = ParameterDirection.Output; SqlCmd.Parameters.Add(ParId); SqlParameter ParCode = new SqlParameter(); ParCode.ParameterName = "@code"; ParCode.SqlDbType = SqlDbType.VarChar; ParCode.Size = 50; ParCode.Value = product.Code; SqlCmd.Parameters.Add(ParCode); SqlParameter ParName = new SqlParameter(); ParName.ParameterName = "@name"; ParName.SqlDbType = SqlDbType.VarChar; ParName.Size = 50; ParName.Value = product.Name; SqlCmd.Parameters.Add(ParName); SqlParameter ParDesc = new SqlParameter(); ParDesc.ParameterName = "@description"; ParDesc.SqlDbType = SqlDbType.VarChar; ParDesc.Size = 1024; ParDesc.Value = product.Description; SqlCmd.Parameters.Add(ParDesc); SqlParameter ParImage = new SqlParameter(); ParImage.ParameterName = "@image"; ParImage.SqlDbType = SqlDbType.Image; ParImage.Value = product.Image; SqlCmd.Parameters.Add(ParImage); SqlParameter ParIdCategory = new SqlParameter(); ParIdCategory.ParameterName = "@idcategory"; ParIdCategory.SqlDbType = SqlDbType.Int; ParIdCategory.Value = product.IdCategory; SqlCmd.Parameters.Add(ParIdCategory); SqlParameter ParIdPresentation = new SqlParameter(); ParIdPresentation.ParameterName = "@idpresentation"; ParIdPresentation.SqlDbType = SqlDbType.Int; ParIdPresentation.Value = product.IdPresentation; SqlCmd.Parameters.Add(ParIdPresentation); //Execute the command resp = SqlCmd.ExecuteNonQuery() == 1 ? "OK" : "Not Inserted"; } catch (Exception ex) { resp = "Something went wrong: " + ex.Message; } finally { if (SqlCon.State == ConnectionState.Open) { SqlCon.Close(); } } return(resp); }