Click here to Skip to main content
15,843,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some records in db for a table ProductCategory.
On one window form i have loaded the records and easily can c through navigation button. (First, Next, Previous, Last)
I have loaded a record say ID=5 on this form.
Now i go back to db and delete that record.
I can still seeing ID=5 on my form.
Now when i am trying to update this record i got OptimisticConcurrencyException.
How can i handle such scenerio in my application when there is a record on client side which has already deleted by some one else.


Typical solution for this is a re-fetch using the known primary key. Based on the fetch you'll either get the new row (if it's updated) or you get information that the row exists no more. You can do the re-fetch where you handle the exception.
Share this answer
You can use the Exists keyword at the Where clause in your update query. Here is the MSDN documentation. The example shows select, but this can be used with update as well. It checks the existence before update.[^]

Some example here as well.[^]

After updates as Mika said re fetch the results, so that you will get the updated set.
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