I have this windows form that submits to an access database, when I run the project and start adding new records, it works as expected for the for the first three items I insert and get the following error:
concurrency System.Data.DBConcurrencyException was unhandled
Message="Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."
at NewSRC.SRC_ProgrameObDataSetTableAdapters.TableAdapterManager.UpdateAll(SRC_ProgrameObDataSet dataSet) in C:\SRC1\NewSRC\NewSRC\SRC_ProgrameObDataSet.Designer.vb:line 9975.
I am the only one working with the access database file, so there isn't any chance of multiple insertion, below is my code to save. Thank you in advance
Private Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click Try Me.Validate() Me.CollectionFormBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll(Me.SRC_ProgrameObDataSet) objConnection.Open() FillDatagrid() ToolStripStatusLabel1.ForeColor = Color.Green ToolStripStatusLabel1.Text = "Submitted Succesfully" Catch ex As Exception ToolStripStatusLabel1.ForeColor = Color.Red ToolStripStatusLabel1.Text = "Error Submitting" MsgBox(ex.ToString) End Try
Sunday, June 17, 2012 4:55 PM
Welcome to the MSDN forum.
Please take a look at the following relies by Mike and Alex in this reference link
When you get a Concurrency Violation it indicates that the data in the database and data in the datatable are not synchronized. For example, the row in the datatable that you're updating no longer exists in the database, or the row’s value in the database has already been changed by another user which conficts with the original value of the row in datatable. You mentioned in your original post that “when I deleted one of the rows in the target then I received the exception "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."” , so I think that the reason why you got this exception is probably that the row in the datatable you are going to update no longer exists in the database. Please consider the necessity to delete that row in the target database, or if you delete it from the database, then do not try to update that row or delete it from the data table too.
As you know, ADO.NET works in disconnected environment. Means one you fetch data from Database, it is possible that someone else (different user) can modify original data source. When you apply your changes back to the DB using an update command, and provider finds this difference in row, you will get concurrency exception. ADO.NET provides different ways to handle this situation.
In general, there are three common ways to manage concurrency in a database:
Pessimistic concurrency control: A row is unavailable to users from the time the record is fetched until it is updated in the database.
Optimistic concurrency control: A row is unavailable to other users only while the data is actually being updated. The update examines the row in the database and determines whether any changes have been made. Attempting to update a record that has already been changed results in a concurrency violation.
"Last in wins": A row is unavailable to other users only while the data is actually being updated. However, no effort is made to compare updates against the original record; the record is simply written out, potentially overwriting any changes made by other users since you last refreshed the records.
Please continue reading here : http://msdn.microsoft.com/en-us/library/cs6hb8k4(VS.80).aspx
I hope this will be helpful to your issue.
MSDN Community Support | Feedback to us
Tuesday, June 19, 2012 7:55 AMModerator
- Marked as answer by Shanks ZenModerator Monday, July 02, 2012 7:25 AM