CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

David Hayden [MVP C#]

         .NET Tutorials, Patterns, and Practices

ObjectDataSource and ConflictDetection - Update Concurrency

I played with the ObjectDataSource Control a bit this weekend to understand how it works with update concurrency for multiuser ASP.NET web applications.  If you are interested in learning about the fundamentals of concurrency in SQL Server at a high level, you can read my post on it:  Concurrency in SQL Server.

Simple ASP.NET Web Application

I put together a simple web page consisting of a GridView connected to an ObjectDataSource using SQL Server Express as the datastore.  The GridView is populated by a Customers Table in the database.

 

 

Business Classes

The ObjectDataSource Control connects to a business class in your web application for handling the database CRUD activities.  I called this class Customers.  Optionally, you can have the ObjectDataSource Control pass a class back and forth to Customers as opposed to individual fields.  I opted for passing a class back and forth, which I have called Customer.

 

 

These classes and methods are specified in the properties of the ObjectDataSource Control:

 

<asp:ObjectDataSource

    ID="ObjectDataSource1"
    TypeName="Customers"
    DataObjectTypeName="Customer"
    DeleteMethod="DeleteCustomer"
    InsertMethod="InsertCustomer"
    SelectMethod="GetCustomers"
    UpdateMethod="UpdateCustomer"
    runat="server">
    
    ...

</asp:ObjectDataSource>

 

ConflictDetection

The ObjectDataSource Control has a property, called ConflictDetection.  This sets how you want to handle concurrency in your application.

 

 

The default setting is OverwriteChanges, which is the least amount of concurrency control you can have in your application.  This is perfect for single user applications or when accidental overwrites of data is acceptable / improbable.  For those times when overwriting data is unacceptable, choose CompareAllValues, which will give you much more flexibility and information on checking for changes in the underlying record.

 

ConflictDetection = Overwrite Changes

When I choose OverwriteChanges, the signature of my Update Method looks like the following:

 

 

The ObjectDataSource Control passes a single updated Customer class to the method.  This class is passed to my data access layer, where any changes made by someone else will be overwritten in the database.

 

ConflictDetection = CompareAllValues

When I choose CompareAllValues, the signature of my Update Method now looks like the following:

 

 

The Update Method accepts 2 Customer Classes - the updated customer information and the original customer information.  Hence we can now pass both classes to the Data Access Layer which can create an SQL Update Query that can specify original values in the WHERE Clause to make sure no data gets overwritten ( see my post Concurrency in SQL Server for more information ).

The Update Method expects the format "original_customer" as the name of the original class based on the OldValuesParameterFormatString Property setting of the ObjectDataSource Control which I have specified as follows:

 

<asp:ObjectDataSource

    ID="ObjectDataSource1"
    TypeName="Customers"
    DataObjectTypeName="Customer"
    DeleteMethod="DeleteCustomer"
    InsertMethod="InsertCustomer"
    SelectMethod="GetCustomers"
    UpdateMethod="UpdateCustomer"
    OldValuesParameterFormatString="original_{0}"
    ConflictDetection="CompareAllValues"
    runat="server">
    
    ...

</asp:ObjectDataSource>

 

Conclusion

The ObjectDataSource Control provides the features to help with update concurrency in your multiuser ASP.NET web applications.  By changing the ConflictDetection and OldValuesParameterFormatString you can get access to both the original class and updated class data to handle concurrency in any number of ways.

 

DrinkingGyokuro Green Tea

 



Comments

Christopher Steen said:

CAB November 2005 Release Ships! [Via: Brad Wilson ]
CAB: ClickOnce and BankTeller [Via: Brad
Wilson...
# November 17, 2005 10:16 PM
Check out Devlicio.us!

Our Sponsors

This Blog

Syndication

News

CodeBetter.Com Home