7.10. Resolving Data Conflicts

Problem

You need to effectively resolve data conflicts and prevent overwriting of existing data when attempting to update changes in a DataSet to a database where the underlying data has changed.

Solution

Handle the DBConcurrencyException within the RowUpdated event of the DataAdapter.

The solution uses a table named ResolveDataConflict in the database AdoDotNet35Cookbook. Execute the following T-SQL statement to create the table:

	USE AdoDotNet35Cookbook
	GO
	CREATE TABLE ResolveDataConflict(
	    Id int NOT NULL PRIMARY KEY,
	    Field1 nvarchar(50) NULL,
	    Field2 nvarchar(50) NULL )

The schema of table ResolveDataConflict is shown in Table 7-6.

Table 7-6. ResolveDataConflict table schema

Column name

Data type

Length

Length

Id

int

4

No

Field1

nvarchar

50

Yes

Field2

nvarchar

50

Yes

Execute the following T-SQL statement to create some sample data for the solution:

	USE AdoDotNet35Cookbook
	GO
	INSERT INTO ResolveDataConflict VALUES (1, 'Field1.1', 'Field2.1');
	INSERT INTO ResolveDataConflict VALUES (2, 'Field1.2', 'Field2.2');
	INSERT INTO ResolveDataConflict VALUES (3, 'Field1.3', 'Field2.3');

The solution creates a DataTable named User 1 and fills it with the schema and data in the table ResolveDataConflict in the AdoDotNet35Cookbook database. The ContinueUpdateOnError property of the DataAdapter is set to true. A handler named daUser1_RowUpdated is assigned to the RowUpdated event of the DataAdapter. A CommandBuilder object is created to generate the updating logic for the DataAdapter.

Get ADO.NET 3.5 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.