Checking for Concurrency Violations


You need to check for concurrency violations.


Use a timestamp column to manage data concurrency violations.

The schema of table TBL0610 used in this solution is shown in Table 6-12.

Table 6-12. TBL0610 schema

Column name

Data type


Allow nulls?













The sample code contains four event handlers and two methods :


Sets up the sample by creating two DataTable objects: A and B . For each, a DataAdapter is created and a handler is attached to handle the RowUpdated event. The DataAdapter is used to fill the DataTable with both schema and data from table TBL0610 in the database. The update command is defined for the DataAdapter using the timestamp column in the WHERE clause to catch concurrency errors. The first row from both tables is displayed on the form simulating the view of two different simultaneous data users.


Checks if the row was inserted or updated. For those rows, the current value of the timestamp column is retrieved from the TBL0610 in the database and used to update the row in the DataTable .

Update( )

This method copies the specified value for Field1 from the text box into the DataTable . The DataAdapter is used to update DataTable changes to table TBL0610 in the database. If a DBConcurrencyException is encountered , the change to the row is rejected.

DisplayRow( )

This method is used to display data from a specified row in DataTable A or B to the corresponding user display on the form.

Update Button.Click

Calls the Update( ) method to update changes made to the row in either DataTable A or B . The DisplayRow( ) method is called to update the corresponding row displayed on the form.

Refresh Button.Click

Gets the latest values for the row in DataTable A or B from table TBL0610 in the database. The DisplayRow( ) method is called to update the corresponding row displayed on the form.

The C# code is shown in Example 6-28.

Example 6-28. File: RowversionForm.cs

// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Windows.Forms;
using System.Text;

using System.Data;
using System.Data.SqlClient;

private DataTable dtA, dtB;
private SqlDataAdapter daA, daB;

private const String TABLENAME = "TBL0610";

// . . . 

private void RowversionForm_Load(object sender, System.EventArgs e)
 // Build statements to select only the row for ID = 1
 // and to update the data for the DataAdapter.
 String selectText = "SELECT Id, Field1, rowversion FROM " +
 String updateText = "UPDATE " + TABLENAME + " " +
 "SET Field1 = @Field1 " +
 "WHERE Id = 1 AND rowversion = @rowversion";

 // Create table A and fill it with the schema.
 dtA = new DataTable("A");
 daA = new SqlDataAdapter(selectText,
 daA.RowUpdated += new SqlRowUpdatedEventHandler(da_RowUpdated);
 daA.FillSchema(dtA, SchemaType.Source);
 dtA.Columns["rowversion"].ReadOnly = false;

 // Create the update command and define the parameters.
 daA.UpdateCommand = new SqlCommand(updateText,
 daA.UpdateCommand.CommandType = CommandType.Text;
 daA.UpdateCommand.Parameters.Add("@Id", SqlDbType.Int, 0, "Id");
 daA.UpdateCommand.Parameters["@Id"].SourceVersion =
 daA.UpdateCommand.Parameters.Add("@Field1", SqlDbType.NVarChar, 50,
 daA.UpdateCommand.Parameters["@Field1"].SourceVersion =
 daA.UpdateCommand.Parameters.Add("@rowversion", SqlDbType.Timestamp, 0,
 daA.UpdateCommand.Parameters["@rowversion"].SourceVersion =

 // Create table B and fill it with the schema.
 dtB = new DataTable("B");
 daB = new SqlDataAdapter(selectText,
 daB.RowUpdated += new SqlRowUpdatedEventHandler(da_RowUpdated);
 daB.FillSchema(dtB, SchemaType.Source);
 dtB.Columns["rowversion"].ReadOnly = false;

 // Create the update command and define the parameters.
 daB.UpdateCommand = new SqlCommand(updateText,
 daB.UpdateCommand.CommandType = CommandType.Text;
 daB.UpdateCommand.Parameters.Add("@Id", SqlDbType.Int, 0, "Id");
 daB.UpdateCommand.Parameters["@Id"].SourceVersion =
 daB.UpdateCommand.Parameters.Add("@Field1", SqlDbType.NVarChar, 50,
 daB.UpdateCommand.Parameters["@Field1"].SourceVersion =
 daB.UpdateCommand.Parameters.Add("@rowversion", SqlDbType.Timestamp, 0,
 daB.UpdateCommand.Parameters["@rowversion"].SourceVersion =

 // Display the first row (ID=1) from both tables on the form.
 DisplayRow(dtA, 0);
 DisplayRow(dtB, 0);

private void da_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
 // Check if an insert or update operation is being performed.
 if(e.Status == UpdateStatus.Continue && 
 (e.StatementType == StatementType.Insert 
 e.StatementType == StatementType.Update))
 // Build a command object to retrieve the updated timestamp.
 String sqlGetRowVersion = "SELECT rowversion FROM " +
 TABLENAME + " WHERE Id = " + e.Row["Id"];
 SqlConnection conn = new SqlConnection(
 SqlCommand cmd = new SqlCommand(sqlGetRowVersion, conn);

 // Set the timestamp to the new value in the data source and
 // call accept changes.
 conn.Open( );
 e.Row["rowversion"] = (Byte[])cmd.ExecuteScalar( );
 conn.Close( );
 e.Row.AcceptChanges( );

private void Update(SqlDataAdapter da, DataTable dt, TextBox field1TextBox)
 // Move the value for the field named Field1
 // from the form to the DataTable for updating.
 dt.Rows[0]["Field1"] = field1TextBox.Text;

 // Update the row table.
 catch (DBConcurrencyException ex)
 // Error if timestamp does not match
 dt.RejectChanges( );

private void DisplayRow(DataTable dt, int index)
 if (dt.TableName == "A")
 aIdTextBox.Text = dt.Rows[index]["Id"].ToString( );
 aField1TextBox.Text = dt.Rows[index]["Field1"].ToString( );
 aRowversionTextBox.Text = Convert.ToBase64String(
 bIdTextBox.Text = dt.Rows[index]["Id"].ToString( );
 bField1TextBox.Text = dt.Rows[index]["Field1"].ToString( );
 bRowversionTextBox.Text = Convert.ToBase64String(

private void updateButton_Click(object sender, System.EventArgs e)
 if(((Button)sender).Tag.ToString( ) == "A")
 Update(daA, dtA, aField1TextBox);
 DisplayRow(dtA, 0);
 Update(daB, dtB, bField1TextBox);
 DisplayRow(dtB, 0);

private void refreshButton_Click(object sender, System.EventArgs e)
 if(((Button)sender).Tag.ToString( ) == "A")
 DisplayRow(dtA, 0);
 DisplayRow(dtB, 0);


A timestamp is a data type that automatically generates an 8-byte binary value guaranteed to be unique within the database.

The T-SQL timestamp data type is not the same as the timestamp data type defined in the SQL-92 standard. The timestamp data type defined by the SQL-92 standard is equivalent to the T-SQL datetime data type.

SQL Server 2000 introduced the data type synonym rowversion for the timestamp data type and recommends its use wherever possible in DDL statements. One difference between the timestamp data type and its data type synonym rowversion is that the timestamp data type does not require a column name while the rowversion data type does.

A table can have only one timestamp column. The value of the timestamp field is updated with the current database timestamp value from the @@DBTS function each time a row having a timestamp column is inserted or updated. This makes it a poor choice as part of a key, especially a primary key, since the index would be rebuilt with each row inserted or updated and the performance would suffer.

The timestamp value is not related to the date and time that data is inserted or updated. To record the time of record inserts and updates, define a datetime column in the table and create update and insert triggers to set its value.

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax

ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: