Click here to Skip to main content
15,889,335 members
Articles / General Programming / Exceptions
Tip/Trick

Concurrency violation: the UpdateCommand affected 0 of the expected 1 records

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
4 Dec 2009CPOL 40.1K   3   3
hi all.i've just encountered a problem in C#:"Concurrency violation: the UpdateCommand affected 0 of the expected 1 records".that is an exception raised when there is a mismatch between the time that the DataTable reads from the DB until the time it wants to update it.one can use 2...
hi all.
i've just encountered a problem in C#:
"Concurrency violation: the UpdateCommand affected 0 of the expected 1 records".


that is an exception raised when there is a mismatch between the time that the DataTable reads from the DB until the time it wants to update it.

one can use 2 functions: dataAdapter.Fill(dataTable) and dataAdapter.Update(dataTable)

my problem was that i 1st made Fill(), then made changes to the table, than 2nd made Update(). until now it worked perfect. now when i want to make further Update() calls i get this Exception.

after some hours...i've figured out that all u need to do is use dataTable.Clear() and than call the Fill() method again and it works perfect!

License

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


Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAre you sure that this work? I'm using .NET4 and SQLite and ... Pin
real-insanity14-Dec-11 2:29
real-insanity14-Dec-11 2:29 
GeneralMeaning in that order.You Clear(),Fill() and Update() Pin
Member 81391345-Aug-11 4:46
Member 81391345-Aug-11 4:46 
GeneralConcurrency violation case. Pin
Navneet Hegde7-Dec-09 1:59
Navneet Hegde7-Dec-09 1:59 
Hi!

I have the same problem but little differnt case.
I fill the DATATABLE using ADAPTER.
and traverse through the ROWS for if say for example ColumnA has value less than 5 then I set that value to 50
now I have updated DATATABLE, so now when I try to UPDATE it back to DB
it gives me ERROR[Concurrency violation] since someone else has changed the original value in DB.
How can I tackle this case.
as per your trick I again have to FILL the datatable and again EDIT and UPDATE it back to DB.

Is that the only way.

At the moment this procedure is run every 15 minutes. Which makes it possible that DB is updated during a day; But i still see such error in LOG atleast 2-3 times a day.


Thanks!

Develop2Program & Program2Develop

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.