C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Concurrency Control Using ADO.NET


Posted Date: 29 Nov 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: pradiptaMember Level: Gold    
Rating: 1 out of 5Points: 7



While doing certain modification in the database some time you need to lock the data so that no one can else perform modification in that data. There are two commonly known approaches for locking database they are optimistic locking and pessimistic locking.
Both these approaches are used to maintain concurrency in the database. Pessimistic concurrency locking is done at rows of the data source to prevent users from modifying data in a way that affects other users. In a pessimistic model, when a user performs an action that causes a lock to be applied, no one else can perform action until unless owner releases that lock. But this is not case with optimistic currency model. In optimistic concurrency model user does not lock row while reading it, while user only locks the row while updating changes to the database.

In .NET we use DataSet object for modifying changes in the database. The DataSet object uses optimistic concurrency model with the help of DataAdaptor. The DataSet object is designed to encourage the use of optimistic concurrency for long-running activities such as when you are working in distributed environment.

In real time execution DataSet maintains the versions of data that means if anyone modify any data in the DataSet then it get maintain in the dataset as old version and new version. While updating modified data in the database if any of the concurrency conflict occur it raises Exception, which sets DataRow’s HasError Boolean value. This we can easily handle with DataAdaptor event and with our own programming logic.
A simple code sample, which explains you how can you manage, concurrency control in .NET environment


string connectionString = ".......................";
SqlConnection myConnection = new SqlConnection(connectionString);

SqlDataAdapter myAdaptor = new SqlDataAdapter("SELECT Name, City FROM Employee ORDER BY EmpID", myConnection);

// Add the RowUpdated event handler.
myAdaptor.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);

DataSet supplierData = new DataSet();
myAdaptor.Fill(supplierData, "Supplier");

// Modify the DataSet contents.
…………………………………………….

myAdaptor.Update(supplierData, "Supplier");

foreach (DataRow myRow in supplierData.Tables["Supplier"].Rows)
{
if (myRow.HasErrors)
Console.WriteLine(myRow[0] + "\n" + myRow.RowError);
}

protected static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args)
{
if (args.RecordsAffected == 0)
{
args.Row.RowError = "Optimistic Concurrency Violation Encountered";
args.Status = UpdateStatus.SkipCurrentRow;




Responses

Author: critic    29 Nov 2004Member Level: Bronze   Points : 0
This article already exists here : http://www.c-sharpcorner.com/Code/2002/Aug/TransactionsNConcurr.asp.

Please let us know if you own this article.


Author: Nikki Kaushik    16 Dec 2004Member Level: Bronze   Points : 0
it is a good article. it helps me a lot but i want to know how to maintain Concurrency control for SqlDataReader in ASP.NET with Vb.net

Thanks a lot.


Author: Nikki Kaushik    16 Dec 2004Member Level: Bronze   Points : 0
it is a good article. it helps me a lot but i want to know how to maintain Concurrency control for SqlDataReader in ASP.NET with Vb.net

Thanks a lot.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to Access Host Information.
Previous Resource: Show a confirmation pop up message when a button is clicked
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use