Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
When i am trying to update the database table using the Dataadapter.update method in C#, throws the error "concurrency violation : Updated 0 of the expected 1 records".
Please help me how to get this resolved.

Regards,
Ramakrishna

UPDATE from OP:
OP was able to resolve he issue by himself. Posted resolution as one of the answers.
Posted
Updated 10-Dec-19 3:39am
v2

HI All,
I got this resolved by specifying the datatable.acceptchanges().
once this is done set added property of each row to true and you are there with the correct working of application.
Thanks all for the replies.
 
Share this answer
 
Hi All,
This blog http://blogs.msdn.com/b/spike/archive/2010/04/07/concurrency-violation-the-updatecommand-affected-0-of-the-expected-1-records.aspx[^]
defines the cause of the related problem, however most specifically the cause can be best understand from the msdn http://msdn.microsoft.com/en-us/library/cs6hb8k4.aspx[^]

Taken from msdn
Quote:
When an update is attempted, the original version of a changed row is compared against the existing row in the database. If the two are different, the update fails with a concurrency error. It is up to you at that point to reconcile the two rows, using business logic that you create.


An example to illustrate
In below SQL 'modifiedOn' datatime column has been retrived and has been changed to varchar to display it the users
Quote:
select top 1 Name, Role, convert(varchar, modifiedOn, 105) as modifiedOn from TableName

Now when the UI side user updates the row lets say 'Name' and do an row update on the DB, it will fail giving the concurrency violation error, because of the column 'modifiedOn' it is not modified and it is different against the existing row in the database.
 
Share this answer
 
Hey Ramakrishna,

Most of the time's I've experienced this exception was when one or more columns in the dataset (or datatable) to be updated are automated, or the database gives one or more fields different values due to triggers.

I had tables with a DateCreated and a DateModified field. The DateModified field was populated using a update trigger in the SQL Server database. This caused the concurrency voilation. I hope this helps.

Good Luck,
Eduard
 
Share this answer
 
Comments
Member 11476505 2-Sep-15 1:56am    
REally helpful thank you soooo much
Eduard Keilholz 16-Dec-15 3:08am    
Glad it could help :)
Have a look at this[^] blog - this should give you enough info to get started with the resolution. Follow the references given at the bottom of the blog.
 
Share this answer
 
I had this issue after using an my Update statement for my TableAdapter.

I solved it by refilling my TableAdapter again after the Update method. It then retrieved the data again.

It does the because we are using "Optimistic Concurrency" which you can disable in Advanced Options when configuring the table adapter in the DataSet designer.
 
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