Hi,
I have the following code.
Code Snippet
Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnUpdate.Click
Dim a As DataTable = CType(DgvCnts.DataSource, DataTable)
Dim changedRows As New ArrayList ' retrieve changed rows
Dim changedIndex As New ArrayList ' retrieve the row index changed
For Each row As DataRow In a.Rows
If row.RowState <> DataRowState.Unchanged Then
'Get the index of the row that is changed
changedIndex.Add(a.Rows.IndexOf(row))
MsgBox(a.Rows.IndexOf(row))
changedRows.Add(row)
End If
Next
If changedRows.Count = 0 Then
Return
End If
Dim builder As New SqlCommandBuilder(adapter)
Dim rows() As DataRow = CType(changedRows.ToArray(GetType(DataRow)),
DataRow())
Dim counter As Integer = 0
Dim counter1 As Integer = 0
MsgBox(changedIndex.Count)
adapter.UpdateCommand = New SqlCommand("UPDATE table1 SET
(CT1,CT2,CT3,CT4) VALUES(@j1,@f1,@t1,@s1)", Main.new_connection)
For counter = 0 To changedIndex.Count - 1
''a.Rows(changedIndex.Item(counter)).SetParentRow(rows(counter))
a.Rows(changedIndex.Item(counter)).SetParentRow(rows(counter))
' adapter.UpdateCommand.Parameters.AddWithValue("@j1",
rows(counter).Item(0))
' adapter.UpdateCommand.Parameters.AddWithValue("@f1",
rows(counter).Item(1))
' adapter.UpdateCommand.Parameters.AddWithValue("@t1",
rows(counter).Item(2))
' adapter.UpdateCommand.Parameters.AddWithValue("@s1",
rows(counter).Item(3))
'adapter.Update(a)
Next
End Sub
The table i have does not have a primary key. When i click on update,
the row that was modified needs to be updated in the database too.
However, currently the update is reflected only on the datagrid but
not on the actual database. To my knowledge, to use the Update
function the table requires a primary key. How do I make the update on
the actual database (no primary key for table)? Hope someone can help
me out here.
Thanks!