Optimistic Concurrency – Solving Conflicts 

This demo shows a way to handle conflicts with optimistic concurrency in ADO.NET. You will have to import all the three files into a Windows .NET application and set frmOptimist as the Startup page.

It is based upon a solution in David Sceppa, Microsoft ADO.NET, Microsoft Press 2002, but it has been improved (?) and enlarged.

The solution uses a separate table TestTable in the Northwind SQL Server database. This table is created automatically if you select to recreate the table when the program starts. You will have to enter your own connection string information for cnNorthwind.

This solution uses strongly typed datasets in order to easily carry primary key info across, which is a requirement for the Merge method..

When the program starts, the data from the table have been read from the DB into the dsNorthwind dataset. Three listview controls show the current values in the dataset, the original values in the dataset, and the values in the DB. At that point the values should all be the same.

Pressing the button Modify Data simulates five changes in the dataset (rows 2, 3, 5, 6, and 7). The new values will of course only appear in the top left listview (current dataset values).

Pressing the button Simulate 2nd User simulates that another user manipulates the data by changing rows 3 and 5 and deleting rows 6 and 7. These changes will appear in the bottom right listview (DB values)

Pressing the button Update DB now lets the 1st user try to update the DB from the dataset. This will lead to conflicts for rows 3, 5, 6 and 7. Where the DB now holds different data (rows 3 and 5), these data are loaded into a new Conflict datadet, the current contents of which is shown in the bottom left listview.

Now select each of the problem rows in the top left listview.

For the changed rows 3 and 5, you can choose between accepting the other changes or overwriting them. Note that in either case, the original dataset values in the top right listview is changed to the current DB values (otherwise the possibly upcoming renewed update would not work). The top left dataview of course only reflects those changes you may have accepted.

For the deleted rows you can choose between accepting the deletion and reinstating the row

When you have dealt with all the problem rows, update once again and you should find that your instructions have now been implemented. The original values in the dataset should be the same as those in the DB.

OptDemoExerciseSolution.zip