I have this Datagridview that has a Checkbox column. I also have a button to update the database. What I want to accomplish is to loop all rows that has a checkbox column value will be submitted to the database for updates. The problem is it will only update 1 row at a time. It does not loop to all rows. I tried different techniques to no avail. The UpdateRecord() is just a simple code to update a Boolean field to true or false.
I also have a combobox with list items (Uncleared, Cleared)
Version(1)
Version(2)
Version 3
I want to loop to all records and update the database. All of the above codes will only update 1 row at a time. What am I doing wrong?
I also have a combobox with list items (Uncleared, Cleared)
Version(1)
Code:
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
Dim counter As Integer = 0
If Me.cboisCleared.SelectedIndex = 0 Then -- (0 = Uncleared) code to update all uncleared to true.
For counter = 1 To dgvDisbursements.RowCount
If dgvDisbursements.CurrentRow.Cells("isCleared").Value = True Then
UpdateRecord()
End If
Next
Else (1 = Cleared) --code to update all cleared to false.
For counter = 1 To dgvDisbursements.RowCount
If dgvDisbursements.CurrentRow.Cells("isCleared").Value = False Then
UpdateRecord()
End If
Next
End If
end Sub
Version(2)
Code:
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
If Me.cboisCleared.SelectedIndex = 0 Then --(0 = Uncleared) code to update all uncleared to true.
For Each row As DataGridViewRow In dgvDisbursements.Rows
If row.Cells("isCleared").Value = True Then
UpdateRecord()
End If
Next
Else --(1 = Cleared) code to update all cleared to False.
For Each row As DataGridViewRow In dgvDisbursements.Rows
If row.Cells("isCleared").Value = False Then
UpdateRecord()
End If
Next
End If
End Sub
Code:
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
If cboisCleared.SelectedIndex = 0 Then --(0 = Uncleared) code to update all uncleared to true.
For Each row As DataGridViewRow In dgvDisbursements.Rows
If Boolean.TryParse(row.Cells("isCleared").Value.ToString, isTrue) Then
If isTrue = True Then
UpdateRecord()
End If
End If
Next
Else -- --(1 = Cleared) code to update all cleared to False.
For Each row As DataGridViewRow In dgvDisbursements.Rows
If Boolean.TryParse(row.Cells("isCleared").Value.ToString, isTrue) Then
If isTrue = False Then
UpdateRecord()
End If
End If
Next
End If
End sub
Code:
Private Sub UpdateRecord()
Dim CS As String = ConfigurationManager.ConnectionStrings("SimpleAccounting.My.MySettings.SimpleAcctgConnectionString").ConnectionString
Dim connection As SqlConnection = New SqlConnection(CS)
Dim updateStatment As String = "sproc_tblDisbursements_isCleared_DateCleared_Update"
Dim updateCommand As New SqlCommand(updateStatment, connection)
updateCommand.CommandType = CommandType.StoredProcedure
updateCommand.Parameters.AddWithValue("@DisbursementID", CLng(dgvDisbursements.CurrentRow.Cells("DisbursementID").Value))
updateCommand.Parameters.AddWithValue("@isCleared", CBool(dgvDisbursements.CurrentRow.Cells("isCleared").Value))
updateCommand.Parameters.AddWithValue("@DateCleared", CDate(dgvDisbursements.CurrentRow.Cells("DateCleared").Value))
updateCommand.Parameters.AddWithValue("@UserID", g_UserID)
Try
connection.Open()
updateCommand.ExecuteNonQuery()
Catch ex As SqlException
Throw ex
Finally
connection.Close()
End Try
End Sub
I want to loop to all records and update the database. All of the above codes will only update 1 row at a time. What am I doing wrong?