Concurrency issue

A

A. Varde

Hello there,


I am fairly new to ASP.NET and have been using the data access tutorials at
www.asp.net to get an understanding of some of the basic data access
concepts. I have created a page that uses a detailsview to insert records
into a table and a gridview to edit and delete existing records, so far so
good.

The one problem I have encountered that I have not figured out has to do
with editing records. If another user inserts a record into the table before
I click the edit button the gridview puts the wrong record in editmode ie.


My gridview currently contains the records:

200 Jones

300 Peters

400 Andrews

Another user inserts the following record before I click edit:


100 Walters


I then click the edit button for the record that corresponds to "200 Jones"
(which is the record I want to edit). Once I do this the gridview places the
"100 Walters" record in edit mode. I'm sure I'm missing a fairly basic
concept here but if anyone has a suggestion for me I'd definitely appreciate
it.



Thanks.
 
S

Steven Cheng [MSFT]

Hi Varde,

Regarding on the data editing/updating question you mentioned, it is
actually related to the Data Accessing's concurrent violation handling
topcs. No matter you're dealing with data in ASP.NET or desktop
application, as long as the data is potentially be updated by multiple
client concurrently, we will have to consider such issues.

In .NET framework, we'll consider handling such concurrent violation at
ADO.NET layer. Here are some web articles mentioned some general ideas and
concepts:

#Handling Data Concurrency Using ADO.NET
http://msdn.microsoft.com/en-us/magazine/cc163924.aspx

#Concurrent Model in ADO.NET
http://www.codeguru.com/csharp/.net/net_data/datagrid/article.php/c12969/

Also, in the ASP.NET Data Accesss tutorial, I also find the following one
which is focus on the concurrency handling demonstration:

#Implementing Optimistic Concurrency
http://www.asp.net/Learn/data-access/tutorial-21-cs.aspx

You'll find that the common approach is always compare all the fields of
the record when performing update into database so as to avoid overwrite
the other ones' changes. And the ADO.NET layer can capture such concurrent
error.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
S

Steven Cheng [MSFT]

Hi Varde,

Have you got any progress on this issue or does the information in my last
reply help you some?

If you have any further specific questions, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: (e-mail address removed) (Steven Cheng [MSFT])
Organization: Microsoft
Date: Mon, 30 Jun 2008 03:31:20 GMT
Subject: RE: Concurrency issue
 
A

A. Varde

Hello Steven,

I reviewed the three articles you referenced and have implemented optimistic
concurrency in my web app as it was described in the “Implementing Optimistic
Concurrency†article. If I place a record in edit mode, then have another
user modify the record and update, and then modify the record myself and
attempt to update I can raise the DBConcurrencyException in the RowUpdated
event of the gridview. I also noticed the following in this article:

“Optimistic concurrency control works by ensuring that the record being
updated or deleted has the same values as it did when the updating or
deleting process started. For example, when clicking the Edit button in an
editable GridView, the record's values are read from the database and
displayed in TextBoxes and other Web controls. These original values are
saved by the GridView. Later, after the user makes her changes and clicks the
Update button, the original values plus the new values are sent to the
Business Logic Layer, and then down to the Data Access Layer. The Data Access
Layer must issue a SQL statement that will only update the record if the
original values that the user started editing are identical to the values
still in the database.â€

Based on my original example I think the following is occurring.

1] My gridview currently contains the following records selected from a table


Gridview Index | ID (Primary Key) | Name



0 | 200 | Jones

1 | 300 | Peters

2 | 400 | Andrews



2] “Another user†inserts a new record into the table with an ID of 100 and
Name equal to Walters and commits



3] I click the Edit button for record “0 | 200 | Jonesâ€



4] A postback occurs and the records are reread from the database (now the
original values) and placed in the gridview as follows



Gridview Index | ID (Primary Key) | Name



0 | 100 | Walters

1 | 200 | Jones

2 | 300 | Peters

3 | 400 | Andrews

Notice that the new record now has a gridview index of 0, and the one that I
clicked on to edit now has a gridview index of 1.

5] “0 | 100 | Walters†(the wrong record) is placed in edit mode. I can
modify this record and update without raising an exception because the
original values have not changed since they were reread from the database.



I am wondering how can you ensure the correct record is placed in edit mode
regardless of records inserted or deleted by other users between the time
that I initially loaded the page and when I click edit on the record I want
to modify? Thanks for your help.


Steven Cheng said:
Hi Varde,

Have you got any progress on this issue or does the information in my last
reply help you some?

If you have any further specific questions, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: (e-mail address removed) (Steven Cheng [MSFT])
Organization: Microsoft
Date: Mon, 30 Jun 2008 03:31:20 GMT
Subject: RE: Concurrency issue
Hi Varde,

Regarding on the data editing/updating question you mentioned, it is
actually related to the Data Accessing's concurrent violation handling
topcs. No matter you're dealing with data in ASP.NET or desktop
application, as long as the data is potentially be updated by multiple
client concurrently, we will have to consider such issues.

In .NET framework, we'll consider handling such concurrent violation at
ADO.NET layer. Here are some web articles mentioned some general ideas and
concepts:

#Handling Data Concurrency Using ADO.NET
http://msdn.microsoft.com/en-us/magazine/cc163924.aspx

#Concurrent Model in ADO.NET
http://www.codeguru.com/csharp/.net/net_data/datagrid/article.php/c12969/

Also, in the ASP.NET Data Accesss tutorial, I also find the following one
which is focus on the concurrency handling demonstration:

#Implementing Optimistic Concurrency
http://www.asp.net/Learn/data-access/tutorial-21-cs.aspx

You'll find that the common approach is always compare all the fields of
the record when performing update into database so as to avoid overwrite
the other ones' changes. And the ADO.NET layer can capture such concurrent
error.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#noti f
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng [MSFT]

Hi Varde,

Glad to hear from you.

Now I've got your actual concern based on the further description. The
problem you encounter is due to the gridview's editing/updating rely on the
RowIndex rather than the data column fields(primary key or other columns).
Actually, this problem may also occur when you perform edit in GridView and
the Rows are displayed based on sorting of some certain columns in the
resultset.

So far, the best solution to address such issue is always make sure that
you use the primary key to identify the record that will be edited. And for
ASP.NET , you can use an additional DetailsView/FormView control to edit
the detailed info of a certain record. And it is very easy to associate the
DetailsView/FormView with the Gridview control's current Row. The following
tutorial also introducing this:

#Master/Detail Using a Selectable Master GridView with a Details DetailView
http://www.asp.net/learn/data-access/tutorial-10-cs.aspx

Thus, in the detailsView/FormView, you are confident that the row is the
correct one(as it is bound via primary key selection from database).

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Thread-Topic: Concurrency issue
Subject: RE: Concurrency issue
Date: Wed, 2 Jul 2008 10:00:02 -0700
Hello Steven,

I reviewed the three articles you referenced and have implemented optimistic
concurrency in my web app as it was described in the “Implementing Optimistic
Concurrency�article. If I place a record in edit mode, then have another
user modify the record and update, and then modify the record myself and
attempt to update I can raise the DBConcurrencyException in the RowUpdated
event of the gridview. I also noticed the following in this article:

“Optimistic concurrency control works by ensuring that the record being
updated or deleted has the same values as it did when the updating or
deleting process started. For example, when clicking the Edit button in an
editable GridView, the record's values are read from the database and
displayed in TextBoxes and other Web controls. These original values are
saved by the GridView. Later, after the user makes her changes and clicks the
Update button, the original values plus the new values are sent to the
Business Logic Layer, and then down to the Data Access Layer. The Data Access
Layer must issue a SQL statement that will only update the record if the
original values that the user started editing are identical to the values
still in the database.�
Based on my original example I think the following is occurring.

1] My gridview currently contains the following records selected from a table


Gridview Index | ID (Primary Key) | Name



0 | 200 | Jones

1 | 300 | Peters

2 | 400 | Andrews



2] “Another user�inserts a new record into the table with an ID of 100 and
Name equal to Walters and commits



3] I click the Edit button for record � | 200 | Jones�
4] A postback occurs and the records are reread from the database (now the
original values) and placed in the gridview as follows



Gridview Index | ID (Primary Key) | Name



0 | 100 | Walters

1 | 200 | Jones

2 | 300 | Peters

3 | 400 | Andrews

Notice that the new record now has a gridview index of 0, and the one that I
clicked on to edit now has a gridview index of 1.

5] � | 100 | Walters�(the wrong record) is placed in edit mode. I can
modify this record and update without raising an exception because the
original values have not changed since they were reread from the database.



I am wondering how can you ensure the correct record is placed in edit mode
regardless of records inserted or deleted by other users between the time
that I initially loaded the page and when I click edit on the record I want
to modify? Thanks for your help.


:
 
S

Steven Cheng [MSFT]

Hi Varde,

How are you doing?

Have you got any progress on this issue or does the information in my last
reply help you some? Since we're still monitoring the thread, if there is
anything else we can help you, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: (e-mail address removed) (Steven Cheng [MSFT])
Organization: Microsoft
Date: Thu, 03 Jul 2008 04:36:31 GMT
Subject: RE: Concurrency issue
Hi Varde,

Glad to hear from you.

Now I've got your actual concern based on the further description. The
problem you encounter is due to the gridview's editing/updating rely on the
RowIndex rather than the data column fields(primary key or other columns).
Actually, this problem may also occur when you perform edit in GridView and
the Rows are displayed based on sorting of some certain columns in the
resultset.

So far, the best solution to address such issue is always make sure that
you use the primary key to identify the record that will be edited. And for
ASP.NET , you can use an additional DetailsView/FormView control to edit
the detailed info of a certain record. And it is very easy to associate the
DetailsView/FormView with the Gridview control's current Row. The following
tutorial also introducing this:

#Master/Detail Using a Selectable Master GridView with a Details DetailView
http://www.asp.net/learn/data-access/tutorial-10-cs.aspx

Thus, in the detailsView/FormView, you are confident that the row is the
correct one(as it is bound via primary key selection from database).

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#noti f
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Thread-Topic: Concurrency issue
Subject: RE: Concurrency issue
Date: Wed, 2 Jul 2008 10:00:02 -0700
Hello Steven,

I reviewed the three articles you referenced and have implemented optimistic
concurrency in my web app as it was described in the “Implementing Optimistic
Concurrency�article. If I place a record in edit mode, then have another
user modify the record and update, and then modify the record myself and
attempt to update I can raise the DBConcurrencyException in the RowUpdated
event of the gridview. I also noticed the following in this article:

“Optimistic concurrency control works by ensuring that the record being
updated or deleted has the same values as it did when the updating or
deleting process started. For example, when clicking the Edit button in an
editable GridView, the record's values are read from the database and
displayed in TextBoxes and other Web controls. These original values are
saved by the GridView. Later, after the user makes her changes and clicks the
Update button, the original values plus the new values are sent to the
Business Logic Layer, and then down to the Data Access Layer. The Data Access
Layer must issue a SQL statement that will only update the record if the
original values that the user started editing are identical to the values
still in the database.�
Based on my original example I think the following is occurring.

1] My gridview currently contains the following records selected from a table


Gridview Index | ID (Primary Key) | Name



0 | 200 | Jones

1 | 300 | Peters

2 | 400 | Andrews



2] “Another user�inserts a new record into the table with an ID of
100
and
Name equal to Walters and commits



3] I click the Edit button for record � | 200 | Jones�
4] A postback occurs and the records are reread from the database (now the
original values) and placed in the gridview as follows



Gridview Index | ID (Primary Key) | Name



0 | 100 | Walters

1 | 200 | Jones

2 | 300 | Peters

3 | 400 | Andrews

Notice that the new record now has a gridview index of 0, and the one
that
I
clicked on to edit now has a gridview index of 1.

5] � | 100 | Walters�(the wrong record) is placed in edit mode. I can
modify this record and update without raising an exception because the
original values have not changed since they were reread from the database.



I am wondering how can you ensure the correct record is placed in edit mode
regardless of records inserted or deleted by other users between the time
that I initially loaded the page and when I click edit on the record I want
to modify? Thanks for your help.


:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top