I am currently having problems updating a DataSet via a DataAdapter and CommandBuilder. The following code works when adding new records;
However the following code which was slightly amended to allow an edit process fails with the message;
Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
I must stress that the table programmed for updates does not contain a primary key. I have a feeling that this might be the cause of my problem. If this is the case can anybody please recommend an alternative way of programming this?.
Code:
Dim myOleDbConnection As OleDb.OleDbConnection
Dim myOleDbCommand As OleDb.OleDbCommand
Dim myOleDbCommandBuilder As OleDb.OleDbCommandBuilder
Dim myOleDbDataAdapter As OleDb.OleDbDataAdapter
Dim myDataSet As New System.Data.DataSet
Dim myDataTable As New System.Data.DataTable
Dim myRow As System.Data.DataRow
Dim myColumn As System.Data.DataColumn
Dim myConnectionString As String
Dim Loop1 As Integer
Try
myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & gProject.ProjectDatabase.Trim & ";" & _
"User Id=admin;Password=;"
'Instatiate the database connection
myOLEDBConnection = New OleDb.OleDbConnection(myConnectionString)
'Create the SQL command
myOLEDBCommand = New OleDb.OleDbCommand("Select * From PHOTOS", _
myOLEDBConnection)
'Open an OLEDB adapter based on the SQL command and use this to
'create a dataset
myOLEDBConnection.Open()
myOleDbDataAdapter = New OleDb.OleDbDataAdapter(myOleDbCommand)
myOleDbDataAdapter.Fill(myDataSet)
'Add the new record
myDataTable = myDataSet.Tables(0)
'Add a new row to this table
myRow = myDataTable.NewRow()
'Populate the fields in the row
For Each myColumn In myDataTable.Columns
For Loop1 = 1 To theColumnsCollection.Count
If (myColumn.ColumnName.Trim = theColumnsCollection(Loop1)) Then
myRow.Item(myColumn.Ordinal) = theDataItems(Loop1 - 1)
Exit For
End If
Next
Next
'Add the new row to the DataTable rows collection
myDataTable.Rows.Add(myRow)
'Create a command builder
myOleDbCommandBuilder = New OleDb.OleDbCommandBuilder(myOleDbDataAdapter)
'Now update the dataset
myOleDbDataAdapter.Update(myDataSet, myDataTable.TableName)
However the following code which was slightly amended to allow an edit process fails with the message;
Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
Code:
myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & mvarDatabaseFile.Trim & ";" & _
"User Id=admin;Password=;"
mySQLString = "Select * From PHOTOS Where PHOTOS.ID=0"
'Instatiate the database connection
myOleDbConnection = New OleDb.OleDbConnection(myConnectionString)
'Create the SQL command
myOleDbCommand = New OleDb.OleDbCommand(mySQLString.Trim, myOleDbConnection)
'Open an OLEDB adapter based on the SQL command and use this to
'create a dataset
myOleDbConnection.Open()
myOleDbDataAdapter = New OleDb.OleDbDataAdapter(myOleDbCommand)
myOleDbDataAdapter.Fill(myDataSet)
'Add the new record
myDataTable = myDataSet.Tables(0)
'Cycle through all of the returned rows
If (myDataTable.Rows.Count > 0) Then
For Each myRow In myDataTable.Rows
.. make amendments
Next
End If
'Create a command builder
myOleDbCommandBuilder = New OleDb.OleDbCommandBuilder(myOleDbDataAdapter)
'Now update the dataset
If (myDataSet.HasChanges) Then
myOleDbDataAdapter.Update(myDataSet, myDataTable.TableName)
End If
I must stress that the table programmed for updates does not contain a primary key. I have a feeling that this might be the cause of my problem. If this is the case can anybody please recommend an alternative way of programming this?.