tbbrhun
asked on
Cannot update data back to Access database from ADO.NET Dataset object
I'm trying to modify some data I pulled from an Access database into ADO.NET MyDataSet object, and post changes back to the database.
Below is the code from which I removed unnecessary details. The Error I get reads:
5: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
or
5: Update unable to find TableMapping['Table'] or DataTable 'Table'.
depending which version of overloaded DataAdapter.Update method I use
The line cuasing error is
DataAdapter.Update(MyDataS et, "Actions")
This is the entire code:
Dim ConnectionString As String = "MyCorrectConnectionString "
'Open Connection based on connection string provided
Dim CONN As New OleDb.OleDbConnection(Conn ectionStri ng)
'Declare and create new DataSet called "dataSetName"
Dim MyDataSet As New DataSet("dataSetName")
'Declare DataAdapter
Dim DataAdapter As OleDb.OleDbDataAdapter
'Declare Command
Dim CMD As New OleDb.OleDbCommand()
Dim rowEntry As DataRow
'Open connection to the database
CONN.Open()
'Set Command's properties
With CMD
.CommandText = "Select * from Actions"
.CommandType = CommandType.Text
.Connection = CONN
End With
'Create DataAdapter based on the Command
DataAdapter = New OleDb.OleDbDataAdapter(CMD )
'Fill DataAdapter with "Actions" table
DataAdapter.Fill(MyDataSet , "Actions")
For Each rowEntry In MyDataSet.Tables("Actions" ).Rows
With rowEntry
.BeginEdit() 'put in edit mode to avoide raising events
.Item("Comments") = "My Comment"
.EndEdit() 'exit the edit mode
End With
Next
If MyDataSet.HasChanges() Then
Try
DataAdapter.Update(MyDataS et, "Actions") 'this is the error line
'DataAdapter.Update(MyData Set) 'another version thereof
Catch Exc As Exception
'this is where the code goes
MessageBox.Show(Err.Number .ToString & ": " & Err.Description)
Finally
End Try
End If
CONN.Close
CONN.Dispose()
Anyone knows how to fix it?
thanx!
Below is the code from which I removed unnecessary details. The Error I get reads:
5: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
or
5: Update unable to find TableMapping['Table'] or DataTable 'Table'.
depending which version of overloaded DataAdapter.Update method I use
The line cuasing error is
DataAdapter.Update(MyDataS
This is the entire code:
Dim ConnectionString As String = "MyCorrectConnectionString
'Open Connection based on connection string provided
Dim CONN As New OleDb.OleDbConnection(Conn
'Declare and create new DataSet called "dataSetName"
Dim MyDataSet As New DataSet("dataSetName")
'Declare DataAdapter
Dim DataAdapter As OleDb.OleDbDataAdapter
'Declare Command
Dim CMD As New OleDb.OleDbCommand()
Dim rowEntry As DataRow
'Open connection to the database
CONN.Open()
'Set Command's properties
With CMD
.CommandText = "Select * from Actions"
.CommandType = CommandType.Text
.Connection = CONN
End With
'Create DataAdapter based on the Command
DataAdapter = New OleDb.OleDbDataAdapter(CMD
'Fill DataAdapter with "Actions" table
DataAdapter.Fill(MyDataSet
For Each rowEntry In MyDataSet.Tables("Actions"
With rowEntry
.BeginEdit() 'put in edit mode to avoide raising events
.Item("Comments") = "My Comment"
.EndEdit() 'exit the edit mode
End With
Next
If MyDataSet.HasChanges() Then
Try
DataAdapter.Update(MyDataS
'DataAdapter.Update(MyData
Catch Exc As Exception
'this is where the code goes
MessageBox.Show(Err.Number
Finally
End Try
End If
CONN.Close
CONN.Dispose()
Anyone knows how to fix it?
thanx!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER