473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Validation controls and DB transactions

Hi

I've been puzzling over this one for a while now. I'm not really sure what
the best and cleanest approach would be here. This is what I want to do.

A User enters some values in a FormView control and submits the form.
A CustomValidator checks the database for duplicate entries. If a duplicate
is found the Page.IsValid property is made false and the user gets the error
message. If the Validation passes the ObjectDataSource control calls the
Insert method (or Update) of the BLL class and does the DB operation.
Standard stuff then.

The problem with the above is that it is not contained within a DB
transaction. If another user entry with the same values occurs in between
the first Validation and Insert functions then an SqlException will be
thrown with a unique constraint error.

I've thought of not using the Validation control and instead relying on a
custom exception based on the SqlException and checking for it in the
ObjectDataSource Inserted and Updated events. I don't like this because it
relies on the database having the correct constraints setup and when the
Inserted event fires the FormView control clears the input controls values.

So my questions are - can I make the CustomValidator and Insert/Update code
participate in the same DB transaction? I can't see how. Or is there another
way of achieving these requirements using sound practises together with the
standard server controls?

Many thanks
Andrew
Jun 25 '07 #1
3 1478
see the SqlTransaction class.

note. just using begin tran will not solve your problem. because your
check for existence and insert are two statements, you will still get
constraint errors.

if you really need to avoid the error (rather than catch and try again)
at the cost of scaling, you need to take an exclusive lock on the read
(this will prevent the other validator from reading the table) and
release at the update.

-- bruce (sqlwork.com)
J055 wrote:
Hi

I've been puzzling over this one for a while now. I'm not really sure what
the best and cleanest approach would be here. This is what I want to do.

A User enters some values in a FormView control and submits the form.
A CustomValidator checks the database for duplicate entries. If a duplicate
is found the Page.IsValid property is made false and the user gets the error
message. If the Validation passes the ObjectDataSource control calls the
Insert method (or Update) of the BLL class and does the DB operation.
Standard stuff then.

The problem with the above is that it is not contained within a DB
transaction. If another user entry with the same values occurs in between
the first Validation and Insert functions then an SqlException will be
thrown with a unique constraint error.

I've thought of not using the Validation control and instead relying on a
custom exception based on the SqlException and checking for it in the
ObjectDataSource Inserted and Updated events. I don't like this because it
relies on the database having the correct constraints setup and when the
Inserted event fires the FormView control clears the input controls values.

So my questions are - can I make the CustomValidator and Insert/Update code
participate in the same DB transaction? I can't see how. Or is there another
way of achieving these requirements using sound practises together with the
standard server controls?

Many thanks
Andrew

Jun 25 '07 #2
Hi Andrew,

I agree to Bruce's suggestion here. Actually making the two operation(query
existing name from db table and insert new record with the name to db
table) in a transaction will only make the two operation set atomic, but
you will still encounter concurrent inserting issue that cause the
duplicated inserting error at inserting time. Also I have discussed with
other DB engineers and this scenario is not quite suitable to implement at
DB level(through transaction or explicit locking , no record to lock).

I think you can consider the following approachs to prevent duplicated
inserting in application code layer:

**Making the data accessing class (do the query and insert) methods a
contral entry point in your application. Thus, you can perform explicit
lock/exclusive against the methods calls. This will hit concurrent
processing performance but is acceptable if there won't frequent occur such
read/inserting operations in your application.

** Add a global inserting id/name list(a limited queue) in your
application, this list will contains all the current/recent item name being
inserted(in sequence). Thus, when you perform inserting, you will need to
check both the DB and this list to see whether there're duplicated existing
record or any one being inserted now.

How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.

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


Jun 26 '07 #3
Hi Andrew,

Have you got any progress or does our suggestion in previous messages help
you some? If there is anything else we can help, please feel free to post
here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hernán Castelo | last post by:
should i to validate all the "Request"s calls like Request.FORM("...") and Request.Cookies("...") ???? if it is so, i have to see inside every "Input" elements like "Text" and even "Hidden"...
14
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
6
by: Stephen | last post by:
Hi, the validation controls dont work on Netscape or Mozilla and only on Internet Explorer why? How do i correct this problem? Thanks
2
by: Martyn Fewtrell | last post by:
Dear All I have a Windows 2003 Server with IIS6 where the validation controls on ASP.Net pages no longer work. I believe it to be specific to the server as if I create an ASP.Net page on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.