Click here to Skip to main content
15,888,239 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I am working in Visual Basic with an Access Database. In my program, I add a new row to the database as follows:

VB
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim NewRow As DataRow
NewRow = ds.Tables(QuizName).NewRow()
NewRow.Item("QuestionNumber") = Row + 2
ds.Tables(QuizName).Rows.Add(NewRow)
da.Update(ds, QuizName)


The table is updated successfully. However, if I immediately after delete the newly created record, an error occurs. Below is the code I use to delete the row from the table:

VB
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables(QuizName).Rows(Row).Delete()
da.Update(ds, QuizName)


Where 'Row' is the location of the newly created row. However, the da.Update line has an error:

DBConcurrencyException was unhandled
Concurrency violation: the DeleteCommand affected 0 of the expected 1 records.


I have no real idea how to fix this problem. It seems that the dataset or the database does not contain the row I want to delete. I can confirm that the access database does contain the row (I open the database after making it, and the new row exists).

Any help remedying this issue would be greatly appreciated!
Posted

You need to reload your DataTable after you do the Add so the record picks up any ID's the database assigned.
 
Share this answer
 
Thanks Dave. I tried that before, but I must have put it in the wrong spot because that essentially solves my problem! I also had to clear the DataSet before filling it again. The solution is in the last two lines of the code below:
VB
NewRow = ds.Tables(QuizName).NewRow()
NewRow.Item("QuestionNumber") = Row + 2
ds.Tables(QuizName).Rows.Add(NewRow)
da.Update(ds, QuizName)
ds.Clear()
da.Fill(ds, QuizName)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900