title: How to update SQL Table Using DataGridView Values in C#?
body: <p><a href="https://i.stack.imgur.com/FQfQB.png" rel="nofollow noreferrer">enter image description here</a>I tried to update selected columns of SQL Database table which from DataGridView. But it said my input string is wrong.So how to fix this.(PO_No is the primary key of PO table and it has identity value and also it is the foreign key of PO_Cart table)</p>
  
  <pre><code>public void UpdatePOCartTable(int PO_No,string ISBN_No,int OrderQuantity, decimal UnitPrice, decimal Total)
      {
          DynamicConnection con = new DynamicConnection();
          con.mysqlconnection();
          string query = "UPDATE TBL_PO_Cart"
              + " SET ISBN_No = @ISBN_No, OrderQuantity= @OrderQuantity,"
              + "UnitPrice= @UnitPrice, Total=@Total"
              + "WHERE PO_No = @PO_No";
          con.sqlquery(query);
          con.cmd.Parameters.Add(new SqlParameter("@PO_No", SqlDbType.Int));
          con.cmd.Parameters["@PO_No"].Value = PO_No;
          con.cmd.Parameters.Add(new SqlParameter("@ISBN_No", SqlDbType.NVarChar));
          con.cmd.Parameters["@ISBN_No"].Value = ISBN_No;
          con.cmd.Parameters.Add(new SqlParameter("@OrderQuantity", SqlDbType.NVarChar));
          con.cmd.Parameters["@OrderQuantity"].Value = OrderQuantity;
          con.cmd.Parameters.Add(new SqlParameter("@UnitPrice", SqlDbType.Money));
          con.cmd.Parameters["@UnitPrice"].Value = UnitPrice;
          con.cmd.Parameters.Add(new SqlParameter("@Total", SqlDbType.Money));
          con.cmd.Parameters["@Total"].Value = Total;
          con.nonquery();
      }
  
  private void btnedit_Click(object sender, EventArgs e)
      {
          DynamicConnection con = new DynamicConnection();
          try
          {
              if (txtPONo.Text != "" || cmbsupID.Text != "" || date1.Text != "" || requireddate.Text != "" || txtgrandTotal.Text != "")
              {
                  PurchaseOrder PO = new PurchaseOrder();
  
                  for (int i = 0; i &lt; dataGridView1.Rows.Count-1; i++)
                  {
  
                      PO.UpdatePOCartTable(Convert.ToInt32(txtPONo.Text),dataGridView1.Rows[i].Cells[1].Value.ToString(), Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value.ToString()), Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value.ToString()), Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value.ToString()));
  
                  }
              }
              else
              {
                  MessageBox.Show("Please Provide Details!");
              }
              dataGridView1.Rows.Clear();
              ClearData();
              retviewPO_No();
              MessageBox.Show("Record Updated Successfully");
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error Occured" + ex.Message);
          }
      }
  </code></pre>