I'm using VB 2008 with an Access database.
A form has a bound datagridview control that has a tableadapter/bindingsource for the its datassource.
I'm having a problem when inserting a new record. The record has a primary key which is an Access autonumber datatype.
There is a dbconcurrencyexception thrown.
I'm the only only user using the database. So the error isn't occuring because another user has accessed the same record.
I've researched the issue and found a solution that required code to be added to dataset xsd file to return the new primary key autonumber field.
This code is working correctly. The correct primary key is returned.
The record is added after the first cell in the grid has a value entered, and the focus moves to the next cell. However, after entering a value for the next cell and moving the focus again, the dbconcurrency error occurs.
I've checked both the datagridview column and the field in the bindingsource
and both have the correct primary key/autonumber value for the new record.
I'm at a loss to find a solution. Code below.
-- This code in the dataset xsd file
A form has a bound datagridview control that has a tableadapter/bindingsource for the its datassource.
I'm having a problem when inserting a new record. The record has a primary key which is an Access autonumber datatype.
There is a dbconcurrencyexception thrown.
System.Data.DBConcurrencyException: Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
I'm the only only user using the database. So the error isn't occuring because another user has accessed the same record.
I've researched the issue and found a solution that required code to be added to dataset xsd file to return the new primary key autonumber field.
This code is working correctly. The correct primary key is returned.
The record is added after the first cell in the grid has a value entered, and the focus moves to the next cell. However, after entering a value for the next cell and moving the focus again, the dbconcurrency error occurs.
I've checked both the datagridview column and the field in the bindingsource
and both have the correct primary key/autonumber value for the new record.
I'm at a loss to find a solution. Code below.
VB.NET:
Private Sub dgvAccessories_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvAccessories.CurrentCellDirtyStateChanged
DirtyAccessory = True
End Sub
Private Sub dgvAccessories_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvAccessories.CellLeave
If Not formLoading AndAlso DirtyAccessory = True Then
SaveAccessory()
UpdateAccessoryCosts(e.RowIndex)
End If
End Sub
Private Sub SaveAccessory()
Try
Validate()
bsProductAccessories.EndEdit()
'Error occurs on the next line
taProductAccessories.Update(DsProducts.ProductAccessories)
DirtyAccessory = False
Catch ex As System.Exception
DirtyAccessory = False
End Try
End Sub
-- This code in the dataset xsd file
VB.NET:
Namespace dsProductsTableAdapters
Partial Class ProductAccessoriesTableAdapter
Private Sub OnRowUpdated(ByVal sender As Object, ByVal args As System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles _adapter.RowUpdated
Try
' Include a variable and a command to retrieve the identity value
' from the Access database.
Dim newID As Integer = 0
Dim idCMD As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand("SELECT @@IDENTITY", Me.Connection) ' _connection)
If args.StatementType = StatementType.Insert Then
' Retrieve the identity value and store it in the CategoryID column.
newID = CInt(idCMD.ExecuteScalar())
args.Row("ProductID") = newID
'args.Row.AcceptChanges()
End If
End Sub
End Class
Last edited: