title: Update command only updates first row vb.net
body: <p>I'm trying to update a database through a datagridview but the problem I'm having is that it only updates the first row in the datagridview. Any insight will be grateful, thanks.</p>
  
  <pre><code>    Dim Connection As New OleDbConnection(Get_Constring)
  
  
      Dim dt As DataTable = New DataTable("SendTable")
      Dim row As DataRow
      dt.Columns.Add("ID", Type.GetType("System.Int32"))
      dt.Columns.Add("Attendance", Type.GetType("System.String"))
      For i = 0 To ClassRegisterdgv.Rows.Count - 1
          ' If ClassRegisterdgv.Rows(i).Cells(4).Value.Equals("") Then ClassRegisterdgv.Rows(i).Cells(4).Value.Equals("Present")
          Dim ID As Integer = ClassRegisterdgv.Rows(i).Cells(0).Value
          Dim Attendance As String = ClassRegisterdgv.Rows(i).Cells(4).Value
          row = dt.Rows.Add
          row.Item("ID") = ID
          row.Item("Attendance") = Attendance
      Next
      If Connection.State = ConnectionState.Closed Then
          Connection.Open()
      End If
      Dim sqlquery As String = "UPDATE PupilInfo SET " &amp; NewColumnCreated &amp; " = @Attendance WHERE ID = @ID"
      Dim sqlcommand As New OleDbCommand
  
      For Each newrow As DataRow In dt.Rows
          'For i = 0 To ClassRegisterdgv.Rows.Count - 1
          With sqlcommand
              .CommandText = sqlquery
              .Parameters.AddWithValue("@Attendance", newrow.Item(1))
              .Parameters.AddWithValue("@ID", newrow.Item(0))
              .Connection = Connection
              MessageBox.Show(newrow.Item(1) &amp; newrow.Item(0))
              .ExecuteNonQuery()
  
          End With
      Next
  
      Connection.Close()
      ClassRegisterdgv.DataSource = Nothing
      dt.Clear()
  </code></pre>