I want to update data using DataGridView in Visual Studio2008 with the following code. But it is not working. Plz help me.

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
cmd = New Odbc.OdbcCommand("UPDATE Table1 SET Name")
Me.Table1TableAdapter.Update(DataSet1.Table1)
MsgBox("Updated")
End Sub

Dani AI

Generated

— the snippet in your first post is incomplete: creating an OdbcCommand with "UPDATE Table1 SET Name" by itself won’t change rows unless that command is associated with a connection, has a proper WHERE clause (usually using the PK), and is executed against the database. As hinted, your command/adapter must be connected; and ’s DataAdapter approach is on the right track but misses a few practical points most people trip on.

Always follow this sequence when updating from a DataGridView bound to a DataSet/BindingSource:

  • Commit the current grid edit to the DataTable (Validate() + BindingSource.EndEdit()).
  • Verify there are changes (DataSet.HasChanges() or DataTable.GetChanges()).
  • Ensure the adapter you call Update on has valid Update/Insert/Delete commands (TableAdapter generated by the designer usually does; a plain DataAdapter needs explicit UpdateCommand or a CommandBuilder and a primary key present).
  • Do not call AcceptChanges() before Update() — that clears row state so nothing will be written.

Useful, minimal VB example showing the commit + explicit UpdateCommand pattern (adjust names and types to your schema):

' commit current grid edit
Me.Validate()
Table1BindingSource.EndEdit()

Dim conn As New Odbc.OdbcConnection(connString)
Dim da As New Odbc.OdbcDataAdapter("SELECT * FROM Table1", conn)

Dim upd As New Odbc.OdbcCommand("UPDATE Table1 SET Name = ? WHERE ID = ?", conn)
upd.Parameters.Add("Name", Odbc.OdbcType.VarChar, 100, "Name")
upd.Parameters.Add("ID", Odbc.OdbcType.Int, 0, "ID")
da.UpdateCommand = upd

Try
    da.Update(DataSet1.Tables("Table1"))
    MessageBox.Show("Updated")
Catch ex As Exception
    MessageBox.Show("Update failed: " & ex.Message)
End Try

If updates still fail, inspect DataSet1.GetChanges() to see what rows are marked modified, catch and display the exception message, and verify the table has a defined primary key (required for generating a WHERE clause). For background reading see BindingSource.EndEdit and the ADO.NET guide on .

Recommended Answers

All 2 Replies

your command and table adapter are not associated.

I want to update data using DataGridView in Visual Studio2008 with the following code. But it is not working. Plz help me.

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
cmd = New Odbc.OdbcCommand("UPDATE Table1 SET Name")
Me.Table1TableAdapter.Update(DataSet1.Table1)
MsgBox("Updated")
End Sub

i'll give you an example how to Update in VB.NET

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
       Dim conn As New Odbc.OdbcConnection("Your Strings Here")
       conn.Open()
       Dim da As New Odbc.OdbcDataAdapter("SELECT * FROM yourTable WHERE Field1 = MyText", conn)
       Dim ds As New DataSet
       Dim cb As New Odbc.OdbcCommandBuilder(da)
       da.Fill(ds)
    
          If ds.Tables(0).Rows.Count = 1 Then
              da.Update(ds)
          MsgBox("Updated")
           End If
     
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.