Hello,
I am facing an unusual situation in which my program executes an update to the SQL database and the database doesn't update properly despite having accurate update statements. I believe this is due to the controls and the data adapter updates.
I have a datagridview which has several columns, two of which are visible. The first visible column is a check box column while the second is a text column.
Say I have four entries displayed, in a manner such as:
checked A
checked B
unchecked C
unchecked D
(where A, B, C, D represent a text value)
If I randomly check/uncheck columns and end with:
unchecked A
checked B
checked C
unchecked D
I then save the data, although the saved data in SQL is not was I was saving.
The debug sql statements show (which are accurate):
UPDATE [Categories] SET [Active] = 0, [CategoryName] = 'Home' WHERE [CategoryID] = 1
UPDATE [Categories] SET [Active] = 1, [CategoryName] = 'Work' WHERE [CategoryID] = 3
UPDATE [Categories] SET [Active] = 1, [CategoryName] = 'School' WHERE [CategoryID] = 4
UPDATE [Categories] SET [Active] = 0, [CategoryName] = 'Office' WHERE [CategoryID] = 5
When the grid is refreshed after the save, the following is displayed:
unchecked A
checked B
unchecked C
checked D
As I am a VB6 programmer and trying to migrate to vb.net, I find this task difficult to master.
All other aspects of the program that save to the database work perfectly, just not this section.
Could anyone provide assistance as to what may be causing this odd behaviour?
Thanks.
If at first you don't succeed, then sky diving wasn't meant for you!
I am facing an unusual situation in which my program executes an update to the SQL database and the database doesn't update properly despite having accurate update statements. I believe this is due to the controls and the data adapter updates.
I have a datagridview which has several columns, two of which are visible. The first visible column is a check box column while the second is a text column.
Say I have four entries displayed, in a manner such as:
checked A
checked B
unchecked C
unchecked D
(where A, B, C, D represent a text value)
If I randomly check/uncheck columns and end with:
unchecked A
checked B
checked C
unchecked D
I then save the data, although the saved data in SQL is not was I was saving.
Code:
dgvCategory.ClearSelection()
btnClose.Focus()
For i = 0 To dgvCategory.Rows.Count - 1
If Not Verify_CategoryUsed(dgvCategory.Rows(i).Cells(2).Value, i + 1) Then
Category_UpdateCommand(i, m_dsCategory.Tables("Categories").Rows(i).Item("CategoryID"), dgvCategory.Rows(i).Cells(1).Value, dgvCategory.Rows(i).Cells(2).Value)
m_daCategory.Update(m_dsCategory, "Categories")
Else
MessageBox.Show("The category '" & dgvCategory.Rows(i).Cells(2).Value & _
"' has already been used." & vbNewLine & vbNewLine & _
"Please enter a different category.", "Invalid Category", MessageBoxButtons.OK, MessageBoxIcon.Warning)
dgvCategory.Rows(i).Cells(2).Value = String.Empty
Exit Sub
End If
Next i
Code:
Private Sub Category_UpdateCommand(ByVal pRow As Integer, ByVal pCatID As Integer, ByVal pActive As Boolean, ByVal pCatname As String)
'// Update a row in the database.
Dim l_strSQL As String
Dim UpdateCmd As SqlCommand
System.Diagnostics.Debug.WriteLine("ID: " & pCatID & ", Active: " & pActive & ", Name: " & pCatname)
l_strSQL = "UPDATE [Categories] SET [Active] = " & Math.Abs(CInt(pActive)) & ", [CategoryName] = '" & pCatname.Trim & "' " & _
"WHERE [CategoryID] = " & pCatID
System.Diagnostics.Debug.WriteLine(l_strSQL)
UpdateCmd = New SqlCommand(l_strSQL, pihCONN)
UpdateCmd.CommandType = CommandType.Text
m_daCategory.UpdateCommand = UpdateCmd
End Sub
The debug sql statements show (which are accurate):
UPDATE [Categories] SET [Active] = 0, [CategoryName] = 'Home' WHERE [CategoryID] = 1
UPDATE [Categories] SET [Active] = 1, [CategoryName] = 'Work' WHERE [CategoryID] = 3
UPDATE [Categories] SET [Active] = 1, [CategoryName] = 'School' WHERE [CategoryID] = 4
UPDATE [Categories] SET [Active] = 0, [CategoryName] = 'Office' WHERE [CategoryID] = 5
When the grid is refreshed after the save, the following is displayed:
unchecked A
checked B
unchecked C
checked D
As I am a VB6 programmer and trying to migrate to vb.net, I find this task difficult to master.
All other aspects of the program that save to the database work perfectly, just not this section.
Could anyone provide assistance as to what may be causing this odd behaviour?
Thanks.
If at first you don't succeed, then sky diving wasn't meant for you!