简体   繁体   中英

How to update SQL Table Using DataGridView Values in C#?

enter image description here 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)

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 < 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);
        }
    }

When Updating SQL I normally use the following code:

String CommandLine = "UPDATE CA_temp_data SET TimePast=@selectTot WHERE TicketNumber=@id";

using (SqlConnection Conn = new SqlConnection("Data Source=" + hostString + ";User ID=" + usernamestring + ";Password=" + sqlpassword))
{
      try
      {
          SqlCommand cmd = new SqlCommand(CommandLine, Conn);

          cmd.Parameters.AddWithValue("@id", ticket);

          cmd.Parameters.AddWithValue("@selectTot", time);

      using (Conn)
      {
            Conn.Open();
            cmd.ExecuteNonQuery();
            Conn.Close();
      }
}
catch (System.Exception excep)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM