delete record from datagrid

ran_deep

New member
Joined
Aug 19, 2003
Messages
2
Location
Malaysia
Guys,
Populated a grid using dataset(connected using ODBC adapter). Now I can update or delete the row in datagrid , but the changes are not reflected in the actual database. Hope someone can provide some snippet on how to do so?

My current coding looks like:

Dim myDS As DataSet = New DataSet()
Dim adapter As New OdbcDataAdapter()

Private Sub frmUsers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New OdbcConnection(strCon)
adapter.SelectCommand = New OdbcCommand("Select * from UsersTable", conn)
adapter.Fill(myDS)
dgUsers.SetDataBinding(myDS, "UsersTable")

End Sub
 
Youll have to use the update command via the adapter as well to write the changes back to the database.

Try:

adapter.Update(myDS)

When you delete/change records in the datagrid, its only changing the in-memory dataset. You still have to write the changes back to the database (maybe on form_closing?).
 
Ive tried that and its giving some error:

An unhandled exception of type System.InvalidOperationException occurred in system.data.dll

Additional information: Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information."

My code:
Private Sub btUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btUpdate.Click
Dim myBuilder As New OdbcCommandBuilder(adapter)
adapter.Update(myDS)
End Sub
 
Back
Top