Results 1 to 3 of 3

Thread: Update method not working

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    9

    Update method not working

    Well, I took out the the exception handler and got this error message at <adpCarriers.Update(xDataSet, "tblcarrier")> :

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

    Additional information: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

    Need help, thanks.

    Code:

    Public Sub ConnectToAccess()
    Dim LastError As Exception


    'LastError = Server.GetLastError()


    Dim conn As New System.Data.OleDb.OleDbConnection
    Dim adpCarriers As New System.Data.oledb.OleDbDataAdapter
    Dim mySelectQuery As String = "SELECT CarrierID, Name FROM tblCarrier"
    'Dim myCommand As New System.Data.OleDb.OleDbCommand(mySelectQuery, conn)
    adpCarriers.SelectCommand = New System.Data.OleDb.OleDbCommand(mySelectQuery, conn)

    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & _
    "F:\tdeseamus\Vendor_Compliance.mdb"
    adpCarriers.TableMappings.Add("Table", "tblCarrier")

    'Try
    conn.Open()
    ' Insert code to process data.
    MsgBox("made it")

    'mydataset.Tables.Add("tblCarrier")
    adpCarriers.Fill(mydataset)
    t = mydataset.Tables(0)
    'DataGrid1.DataSource = t
    'DataGrid1.DataBindings.Add(New Binding("BindingContext", mydataset, "tblcarrier"))
    TextBox1.DataBindings.Add(New Binding("Text", mydataset, "tblcarrier.name"))
    TextBox2.DataBindings.Add(New Binding("Text", mydataset, "tblcarrier.carrierid"))
    MsgBox("made it1")
    mydataset.Tables("tblcarrier").Rows(0).Item("name") = "Severian"
    'mydataset.Tables("tblcarrier").Rows(0).Item("carrierid") = "ZZZZ"
    ' Check for changes with the HasChanges method first.
    If Not mydataset.HasChanges(DataRowState.Modified) Then Exit Sub
    ' Create temporary DataSet variable.
    Dim xDataSet As DataSet
    MsgBox("made it2")
    ' GetChanges for modified rows only.
    xDataSet = mydataset.GetChanges(DataRowState.Modified)
    ' Check the DataSet for errors.
    If xDataSet.HasErrors Then
    ' Insert code to resolve errors.
    End If
    MsgBox("made it3")
    ' After fixing errors, update the data source with the DataAdapter
    ' used to create the DataSet.
    adpCarriers.Update(xDataSet, "tblcarrier")
    MsgBox("Made it4")

    'Catch ex As Exception
    'MsgBox(ErrorNumber)

    'Finally
    conn.Close()
    'End Try
    End Sub

  2. #2

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    9

    Solved it

    Added the line

    adpCarriers.SelectCommand = New System.Data.OleDb.OleDbCommand(mySelectQuery, conn)

    Update method needs this for some reason.

  3. #3
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159
    Code:
    Public Sub ConnectToAccess()       
      Dim ret As Integer
            Try
                Me.SqlConnection1.Open()
                Me.BindingContext(DataSet1, "Products").EndCurrentEdit()
                ret = Me.SqlDataAdapter1.Update(DataSet1.Tables("Products"))
                If (ret <> 0) Then
                    DataSet1.Tables("products").AcceptChanges()
                Else
                    DataSet1.Tables("products").RejectChanges()
                End If
                MessageBox.Show("Records changed: " & ret)
    
            Catch ex As Exception
                DataSet1.Tables("products").RejectChanges()
                MessageBox.Show(ex.Message)
            Finally
                'Me.SqlConnection1.Close()
            End Try
    end sub
    this is update code to update, but i use a SqlConnections and SqlCommand. The concept is simular just change the code to what u need.

    hope it helps if not sorry!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width