troubleshooting Question

how to save data in datagridview

Avatar of mutu
mutuFlag for United States of America asked on
.NET ProgrammingVisual Basic.NET
3 Comments1 Solution4014 ViewsLast Modified:
I have a datagridview that users edit and when the Save button is pressed I want the data from the datagridview to be saved back to the database.  Whenever I press the Save button my application crashes and I get the following error message:

System execution failure
System.InvalidOperationException: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
...

Any ideas on why I'm getting this error?  An update command is defined in my code but not shown in the code snippet.  I've been stuck on this for a couple of days now so any help is appreciated and thank you in advance.


Private dap As New System.Data.OleDb.OleDbDataAdapter()
    Private es As New System.Data.DataSet()
    Private cmd As New System.Data.OleDb.OleDbCommand()
    Private bindingSource1 As New BindingSource()
    Private db As dbConnect = dbConnect.GetInstance()
    Private tblName As String
    Private sortColumn As String
 
    Private Sub frm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        displayGrid()
    End Sub
 
    Sub displayGrid()
            With gridView
                .ReadOnly = False
                .DataSource = eView(txtPartNo.Text).Tables(tblECO)
                .EditMode = DataGridViewEditMode.EditOnEnter
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            End With
        End If
    End Sub
 
    Public Function eView(ByVal itemStr As String) As System.Data.DataSet
        cmd.CommandText = "Select * " + _
          " From " + tblName + _
          " WHERE " + partNo + _
          " LIKE '%" + itemStr + "%'" + _
          " Order by " + partNo
 
        dap = New OleDb.OleDbDataAdapter(cmd.CommandText, db.Connection)
 
        es.Clear()
        dap.TableMappings.Add("Table", tblName)
        dap.Fill(es, tblName)
        Return es
    End Function
 
 
    Public Sub saveGridChanges()
        dap.Update(es)
    End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros