title: Updating product in database
body: <p>I have saved product in my database table that I want to edit, but after editing and hit update the information shown is updated successfully but the new changes does not reflect in the database table after loading the products, tried several times editing but it is not reflecting. The table has <code>pcode</code> as the primary key.</p>
  <p><img src="https://i.stack.imgur.com/uiJdI.png" alt="Database Table" /></p>
  <p><a href="https://i.stack.imgur.com/HJH3O.png" rel="nofollow noreferrer">LoadProduct</a></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Data.SqlClient;
  namespace PointOfSale
  {
      public partial class FrmProduct : Form
      {
      SqlConnection cn = new SqlConnection();
      SqlCommand cm = new SqlCommand();
      DBConnection dbcon = new DBConnection();
      SqlDataReader dr;
      FrmProductList plist;
  
      public FrmProduct(FrmProductList frm)
      {
          InitializeComponent();
          cn = new SqlConnection(dbcon.MyConnection());
          plist = frm;
      }
      private void frmProduct_Load(object sender, EventArgs e)
      {
          GenrateProductCode();
      }
  
      public void LoadBrand()
      {
          cboBrands.Items.Clear();
          cn.Close();
          cn.Open();
          cm = new SqlCommand(&quot;select brand from tblBrand order by brand&quot;, cn);
          dr = cm.ExecuteReader();
          while (dr.Read())
          {
              cboBrands.Items.Add(dr[0]).ToString();
          }
          dr.Close();
          dr.Close();
      }
      public void LoadCategory()
      {
          cboCategory.Items.Clear();
          cn.Close();
          cn.Open();
          cm = new SqlCommand(&quot;select category from tblCategory order by Category&quot;, cn);
          dr = cm.ExecuteReader();
          while (dr.Read())
          {
              cboCategory.Items.Add(dr[0]).ToString();
          }
          dr.Close();
          dr.Close();
      }
  
  
      private void btnClose_Click(object sender, EventArgs e)
      {
          this.Dispose();
      }
  
      private void clear()
      {
          btnSave.Enabled = true;
          btnUpdate.Enabled = false;
         txtBarcode.Clear();
          txtPCode.Clear();
          txtPrice.Clear();
          txtDescription.Clear();
          txtPrice.Clear();
        //  txtReorder.Clear();
          cboBrands.Text = &quot;&quot;;
          cboCategory.Text = &quot;&quot;;
          txtPCode.Focus();
      }
      private void btnSave_Click(object sender, EventArgs e)
      {
          try
          {
              if (MessageBox.Show(&quot;Are you sure to save this Product?&quot;, &quot;Save Product&quot;, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
              {
  
                  string bid = &quot;&quot;; string cid = &quot;&quot;;
                  //Getting the id of brand
                  cn.Close();
                  cn.Open();
                  cm = new SqlCommand(&quot;select id from tblBrand where brand ='&quot; + cboBrands.Text + &quot;'&quot;, cn);
                  dr = cm.ExecuteReader();
                  dr.Read();
                  if (dr.HasRows) { bid = dr[0].ToString(); }
                  dr.Close();
                  cn.Close(); 
                  //getting the id of category
                  cn.Open();
                  cm = new SqlCommand(&quot;select id from tblCategory where category = '&quot; + cboCategory.Text + &quot;'&quot;, cn);
                  dr = cm.ExecuteReader();
                  dr.Read(); 
                  if (dr.HasRows) { cid = dr[0].ToString(); }
                  dr.Close();
                  cn.Close();
                  //Saving Product
                  cn.Open(); 
                  cm = new SqlCommand(&quot;INSERT INTO tblProduct (pcode, barcode, pdesc, bid, cid, price) VALUES(@pcode, @barcode, @pdesc, @bid, @cid, @price)&quot;, cn);
                  cm.Parameters.AddWithValue(&quot;@pcode&quot;, txtPCode.Text);
                  cm.Parameters.AddWithValue(&quot;@barcode&quot;, txtBarcode.Text);
                  cm.Parameters.AddWithValue(&quot;@pdesc&quot;, txtDescription.Text);
                  cm.Parameters.AddWithValue(&quot;@bid&quot;, bid);
                  cm.Parameters.AddWithValue(&quot;@cid&quot;, cid);
                  cm.Parameters.AddWithValue(&quot;@price&quot;, Double.Parse(txtPrice.Text));
                  //cm.Parameters.AddWithValue(&quot;@reorder&quot;, int.Parse(txtReorder.Text));
                  cm.ExecuteNonQuery();
  
                  cn.Close();
                  MessageBox.Show(&quot;Product has been successfully saved.&quot;, &quot;Product Saved&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information);
                  clear();
                  plist.LoadProducts();
              }
  
          }
          catch (Exception ex)
          {
              cn.Close();
              MessageBox.Show(ex.Message);
          }
  
      }
  
      private void btnUpdate_Click(object sender, EventArgs e)
      {
          try
          {
              if (MessageBox.Show(&quot;Are you sure to update this Product?&quot;, &quot;Update Product&quot;, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
              {
  
                  string bid = &quot;&quot;; string cid = &quot;&quot;;
                  //Getting the id of brand
                  cn.Close();
                  cn.Open();
                  cm = new SqlCommand(&quot;select id from tblBrand where brand = '&quot; + cboBrands.Text + &quot;'&quot;, cn);
                  dr = cm.ExecuteReader();
                  dr.Read();
                  if (dr.HasRows) { bid = dr[0].ToString(); }
                  dr.Close();
                  cn.Close();
                  //getting the id of category
                  cn.Open();
                  cm = new SqlCommand(&quot;select id from tblCategory where category = '&quot; + cboCategory.Text + &quot;'&quot;, cn);
                  dr = cm.ExecuteReader();
                  dr.Read();
                  if (dr.HasRows) { cid = dr[0].ToString(); }
                  dr.Close();
                  cn.Close();
                  //Saving Product
                  cn.Open();
                  cm = new SqlCommand(&quot;UPDATE tblProduct SET barcode=@barcode, pdesc=@pdesc, bid=@bid, cid=@cid, price=@price where id = @id&quot; ,cn);
                 
                  cm.Parameters.AddWithValue(&quot;@pcode&quot;, txtPCode.Text);
                  cm.Parameters.AddWithValue(&quot;@barcode&quot;, txtBarcode.Text);
                  cm.Parameters.AddWithValue(&quot;@pdesc&quot;, txtDescription.Text);
                  cm.Parameters.AddWithValue(&quot;@bid&quot;, bid);
                  cm.Parameters.AddWithValue(&quot;@cid&quot;, cid);
                  cm.Parameters.AddWithValue(&quot;@price&quot;, txtPrice.Text);
                 // cm.Parameters.AddWithValue(&quot;@reorder&quot;, txtReorder.Text);
                  cm.ExecuteNonQuery();
  
                  cn.Close();
                  MessageBox.Show(&quot;Product has been successfully updated.&quot;, &quot;Update&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information);
                  clear();
                  plist.LoadProducts();
                  this.Close();
                  
              }
          }
          catch (Exception ex)
          {
              cn.Close();
              MessageBox.Show(ex.Message);
          }
  
      }
      public void LabelID()
      {
  
      }
  
      private void btnCancel_Click(object sender, EventArgs e)
      {
          clear();
      }
  
      private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
      {   if ((e.KeyChar == 46))
          {
              //accept . char
          }
          else if (e.KeyChar == 8) { 
              //accept backspace
          }
          else if ((e.KeyChar &lt; 48) || (e.KeyChar &gt; 57))// ascii code 48-57 between 0-9
          {
              e.Handled = true;
          }
      }
  
      private void txtReorder_KeyPress(object sender, KeyPressEventArgs e)
      {
          if ((e.KeyChar == 46))
          {
              //accept . char
          }
          else if (e.KeyChar == 8)
          {
              //accept backspace
          }
          else if ((e.KeyChar &lt; 48) || (e.KeyChar &gt; 57))// ascii code 48-57 between 0-9
          {
              e.Handled = true;
          }
      }
  
      private void cboBrands_KeyPress(object sender, KeyPressEventArgs e)
      {
          e.Handled = true;
      }
  
      private void cboCategory_KeyPress(object sender, KeyPressEventArgs e)
      {
          e.Handled = true;
      }
  
      private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
      {
          GenrateProductCode();
      }
      private void GenrateProductCode()
      {
          Random rnd = new Random();
          //int i = 0;
          txtPCode.Clear();
          txtPCode.Text = &quot;P-&quot; + DateTime.Now.ToString(&quot;MMyyyy&quot;) + rnd.Next();
      }
  
      private void txtPrice_TextChanged(object sender, EventArgs e)
      {
  
      }
  public void LoadProducts()
          {
              int i = 0;
              dataGridProduct.Rows.Clear();
              cn.Open();
  
              cm = new SqlCommand(&quot;Select p.pcode,p.barcode, p.pdesc, b.brand, c.category, p.price, p.qty from tblProduct as p inner join tblBrand as b on b.id = p.bid inner join tblCategory as c on c.id = p.cid where p.pdesc like '&quot; + txtSearchProduct.Text + &quot;%'&quot;,cn);
  
              dr = cm.ExecuteReader();
  
              while (dr.Read())
              {
                  i++;
                  dataGridProduct.Rows.Add(i,dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(),dr[5].ToString(), dr[6].ToString());
              }
  
              cn.Close();
          }
      }
  }
  </code></pre>