title: How to update rows from database using foreach loop?
body: <p>I am trying to loop through all of the orders from Order table, and update each of the order's delivery status when this function is called. </p>
  
  <pre><code>public void DateCheck()
          {
              if (sqlcon.State == ConnectionState.Closed)
              {
                  sqlcon.Open();
              }
              SqlCommand cmd = new SqlCommand("SELECT * FROM Orders", sqlcon);
              SqlDataAdapter sda = new SqlDataAdapter(cmd);
              DataTable dt = new DataTable();
              sda.Fill(dt);
  
              foreach (DataRow row in dt.Rows)
              {
                  SqlCommand update = new SqlCommand("UPDATE Orders SET D_Status = @D_Status", sqlcon);
                  string deliverytype = row["D_Type"].ToString();
                  DateTime orderdate = DateTime.ParseExact(row["O_Date"].ToString(), "dd/MM/yyyy", null);
                  DateTime deliverydate = new DateTime(); 
  
                  if (deliverytype == "Standard") 
                  {
                      deliverydate = orderdate.AddDays(7);
                  }
                  else if (deliverytype == "Express") 
                  {
                      deliverydate = orderdate.AddDays(5);
                  }
                  else if (deliverytype == "One-Day") 
                  {
                      deliverydate = orderdate.AddDays(1);
                  }
  
                  if (DateTime.Now &gt;= deliverydate)
                  {
                      update.Parameters.AddWithValue("@D_Status", "Delivered");
                      update.ExecuteNonQuery();
                  }
                  else if (DateTime.Now &gt;= deliverydate)
                  {
                      update.Parameters.AddWithValue("@D_Status", "Undelivered");
                      update.ExecuteNonQuery();
                  }
              }
          }
  </code></pre>
  
  <p>However, it updates all of the statuses to "Delivered", even though comparing the code below gives me both "Delivered" and "Undelivered".</p>
  
  <pre><code>(DateTime.Now &gt;= deliverydate)
  </code></pre>
  
  <p>Does anyone have any idea where the code goes wrong? If so, kindly elaborate to help me understand my mistake better. </p>