As shown in the screenshot, I want to update the 3 rows (order #3) in Order Details table by inserting only Qty, Description and Price values from DataGridView2
. I am using the combination of Order_Number and DateTime to make the order details unique and easy to find in the table.
I used the following code but it updates the 3 rows of order #3 based on row 0 in DataGridView2:
private void Update_OrderDetails_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=PCNmm-TOSH;Initial Catalog=mydb;Integrated Security=True");
cn.Open();
SqlCommand cm = new SqlCommand("UPDATE Customer_Order_Details SET Qty = @Qty, Description = @Description, Price = @Price WHERE Order_Number = @OrderNumber and DateTime = @DateTime ");
cm.Parameters.Add("@OrderNumber", SqlDbType.Int);
cm.Parameters["@OrderNumber"].Value = 3;
cm.Parameters.Add("@DateTime", SqlDbType.DateTime);
cm.Parameters["@DateTime"].Value = "2015 - 12 - 17 15:04:57.043";
cm.Parameters.Add("@Qty", SqlDbType.Int);
cm.Parameters["@Qty"].Value = dataGridView2.Rows[0].Cells[2].Value;
cm.Parameters.Add("@Description", SqlDbType.Text);
cm.Parameters["@Description"].Value = dataGridView2.Rows[0].Cells[3].Value;
cm.Parameters.Add("@Price", SqlDbType.Money);
cm.Parameters["@Price"].Value = dataGridView2.Rows[0].Cells[4].Value;
cm.Connection = cn;
cm.ExecuteNonQuery();
}
}
But if I use for
statement for (i = 0; i <= dataGridView2.RowCount; i++)
and use dataGridView2.Rows[i].Cells[Cell_Number].Value
in the parameters
it gives me error "The parameterized query '(@OrderNumber int,@DateTime datetime,@Qty int,@Description text,' expects the parameter '@Qty', which was not supplied"
what I want is:
Row 1
in Order details table = DataGridView2 Row 0
Row 2
in Order details table = DataGridView2 Row 1
Row 3
in Order details table = DataGridView2 Row 2
But the problem is: I do not know how to index those 3 rows in the OrderDetail table in Database. Any idea how can I update the 3 rows (order 3) in database Order Details table with the 3 rows in DataGridView2? Thank you