title: How can I update multiple rows in a table with SQL query?
body: <p>I am new to SQL and C#.
  <a href="https://i.stack.imgur.com/eKi2o.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eKi2o.jpg" alt="screenshot"></a></p>
  
  <p>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 <code>DataGridView2</code>. I am using the combination of Order_Number and DateTime to make the order details unique and easy to find in the table. </p>
  
  <p>I used the following code but it updates the 3 rows of order #3 based on row 0 in DataGridView2:</p>
  
  <pre><code>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();
          }
      }
  </code></pre>
  
  <p>But if I use <code>for</code> statement <code>for (i = 0; i &lt;= dataGridView2.RowCount; i++)</code> and use <code>dataGridView2.Rows[i].Cells[Cell_Number].Value</code> 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"</p>
  
  <p>what I want is:</p>
  
  <p><code>Row 1</code> in Order details table = <code>DataGridView2 Row 0</code></p>
  
  <p><code>Row 2</code> in Order details table = <code>DataGridView2 Row 1</code></p>
  
  <p><code>Row 3</code> in Order details table = <code>DataGridView2 Row 2</code> </p>
  
  <p>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</p>