473,804 Members | 3,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gracefully handling stored procedure errors

One of my developers came to me with this question and I don't have an
answer for them. The only suggestion I had for them was to change the
return to a output parameter and put a try catch around it and look for the
output parameter to have a value.

I would have thought this should have been posted in the sql newgroups, but
I know exactly what they would say, and they would be correct.

But when using asp.net ado.net command object. I am running a sproc that
handles its own errors gracefully and will sets the output parameter @msg to
a successful, or unsucessful message.

And it does return it when I run the sproc in query analyzer.

However, when I run the code in asp.net the Execute method for the Command
object throws and exception so I never get the status.

Is there a way in a stored procedure to clear the error so it doesn't
through an exception through the ado.net object?

bill

<sproc snippet>

Insert Into tblTPA

Values( @FinalID, GetDate(), @InAuditor, @Type, @Comments, @Hours,
@Expenses, @DinersExpenses )

if @@Error <> 0

BEGIN

ROLLBACK

Set @msg = 'The final timesheet you tried to save was already saved by
another user.'

END

else

BEGIN

COMMIT

Set @msg = 'The final timesheet has been saved.'

END

Select @msg
Nov 17 '05 #1
4 1694
William,

The real problem here is that your stored procedure isn't really handling
the error condition "gracefully ". Known potential error conditions that
should not be treated as application exceptions should not be allowed to
occur at all. If you don't want any part of your application treating this
potential duplicate insertion scenario as an exception, test for it in the
stored procedure before the insertion attempt. e.g.:

IF <final timesheet has already been saved>
SET @msg = 'The final timesheet you tried to save was already saved by
another user.'
ELSE
BEGIN
INSERT INTO...
IF @@ERROR <> 0 SET @msg = 'An unexpected error occurred.'
...
END

HTH,
Nicole
"William F. Robertson, Jr." <wf*********@kp mg.com> wrote in message
news:ei******** ******@TK2MSFTN GP09.phx.gbl...
One of my developers came to me with this question and I don't have an
answer for them. The only suggestion I had for them was to change the
return to a output parameter and put a try catch around it and look for the output parameter to have a value.

I would have thought this should have been posted in the sql newgroups, but I know exactly what they would say, and they would be correct.

But when using asp.net ado.net command object. I am running a sproc that
handles its own errors gracefully and will sets the output parameter @msg to a successful, or unsucessful message.

And it does return it when I run the sproc in query analyzer.

However, when I run the code in asp.net the Execute method for the Command
object throws and exception so I never get the status.

Is there a way in a stored procedure to clear the error so it doesn't
through an exception through the ado.net object?

bill

<sproc snippet>

Insert Into tblTPA

Values( @FinalID, GetDate(), @InAuditor, @Type, @Comments, @Hours,
@Expenses, @DinersExpenses )

if @@Error <> 0

BEGIN

ROLLBACK

Set @msg = 'The final timesheet you tried to save was already saved by
another user.'

END

else

BEGIN

COMMIT

Set @msg = 'The final timesheet has been saved.'

END

Select @msg

Nov 17 '05 #2
On Fri, 24 Oct 2003 16:44:49 -0500, "William F. Robertson, Jr."
<wf*********@kp mg.com> wrote:
One of my developers came to me with this question and I don't have an
answer for them. The only suggestion I had for them was to change the
return to a output parameter and put a try catch around it and look for the
output parameter to have a value.

I would have thought this should have been posted in the sql newgroups, but
I know exactly what they would say, and they would be correct.

But when using asp.net ado.net command object. I am running a sproc that
handles its own errors gracefully and will sets the output parameter @msg to
a successful, or unsucessful message.

And it does return it when I run the sproc in query analyzer.

However, when I run the code in asp.net the Execute method for the Command
object throws and exception so I never get the status.

Is there a way in a stored procedure to clear the error so it doesn't
through an exception through the ado.net object?

bill

<sproc snippet>

Insert Into tblTPA

Values( @FinalID, GetDate(), @InAuditor, @Type, @Comments, @Hours,
@Expenses, @DinersExpenses )

if @@Error <> 0

BEGIN

ROLLBACK

Set @msg = 'The final timesheet you tried to save was already saved by
another user.'

END

else

BEGIN

COMMIT

Set @msg = 'The final timesheet has been saved.'

END

Select @msg

Would it be better for the stored proc to raise an exception of it's
own? You can specify the message, to be user friendly, and will be
able to access it in the catch block. This will work with sql server
2K, I wouldn't know about previous versions.

A further point is that there are some errors you can't trap -
duplicate key being one, so if you want nice messages for this the
stored proc must detect that it was going to happen and not try the
insert.
Nov 17 '05 #3
Yes, you were right, that was the real problem.

I should have seen that.

Thanks,
bill

"Nicole Calinoiu" <ni*****@somewh ere.net> wrote in message
news:em******** *****@TK2MSFTNG P10.phx.gbl...
William,

The real problem here is that your stored procedure isn't really handling
the error condition "gracefully ". Known potential error conditions that
should not be treated as application exceptions should not be allowed to
occur at all. If you don't want any part of your application treating this potential duplicate insertion scenario as an exception, test for it in the
stored procedure before the insertion attempt. e.g.:

IF <final timesheet has already been saved>
SET @msg = 'The final timesheet you tried to save was already saved by
another user.'
ELSE
BEGIN
INSERT INTO...
IF @@ERROR <> 0 SET @msg = 'An unexpected error occurred.'
...
END

HTH,
Nicole
"William F. Robertson, Jr." <wf*********@kp mg.com> wrote in message
news:ei******** ******@TK2MSFTN GP09.phx.gbl...
One of my developers came to me with this question and I don't have an
answer for them. The only suggestion I had for them was to change the
return to a output parameter and put a try catch around it and look for the
output parameter to have a value.

I would have thought this should have been posted in the sql newgroups,

but
I know exactly what they would say, and they would be correct.

But when using asp.net ado.net command object. I am running a sproc that
handles its own errors gracefully and will sets the output parameter @msg to
a successful, or unsucessful message.

And it does return it when I run the sproc in query analyzer.

However, when I run the code in asp.net the Execute method for the

Command object throws and exception so I never get the status.

Is there a way in a stored procedure to clear the error so it doesn't
through an exception through the ado.net object?

bill

<sproc snippet>

Insert Into tblTPA

Values( @FinalID, GetDate(), @InAuditor, @Type, @Comments, @Hours,
@Expenses, @DinersExpenses )

if @@Error <> 0

BEGIN

ROLLBACK

Set @msg = 'The final timesheet you tried to save was already saved by
another user.'

END

else

BEGIN

COMMIT

Set @msg = 'The final timesheet has been saved.'

END

Select @msg


Nov 17 '05 #4
Yes, you were right, that was the real problem.

I should have seen that.

Thanks,
bill

"Nicole Calinoiu" <ni*****@somewh ere.net> wrote in message
news:em******** *****@TK2MSFTNG P10.phx.gbl...
William,

The real problem here is that your stored procedure isn't really handling
the error condition "gracefully ". Known potential error conditions that
should not be treated as application exceptions should not be allowed to
occur at all. If you don't want any part of your application treating this potential duplicate insertion scenario as an exception, test for it in the
stored procedure before the insertion attempt. e.g.:

IF <final timesheet has already been saved>
SET @msg = 'The final timesheet you tried to save was already saved by
another user.'
ELSE
BEGIN
INSERT INTO...
IF @@ERROR <> 0 SET @msg = 'An unexpected error occurred.'
...
END

HTH,
Nicole
"William F. Robertson, Jr." <wf*********@kp mg.com> wrote in message
news:ei******** ******@TK2MSFTN GP09.phx.gbl...
One of my developers came to me with this question and I don't have an
answer for them. The only suggestion I had for them was to change the
return to a output parameter and put a try catch around it and look for the
output parameter to have a value.

I would have thought this should have been posted in the sql newgroups,

but
I know exactly what they would say, and they would be correct.

But when using asp.net ado.net command object. I am running a sproc that
handles its own errors gracefully and will sets the output parameter @msg to
a successful, or unsucessful message.

And it does return it when I run the sproc in query analyzer.

However, when I run the code in asp.net the Execute method for the

Command object throws and exception so I never get the status.

Is there a way in a stored procedure to clear the error so it doesn't
through an exception through the ado.net object?

bill

<sproc snippet>

Insert Into tblTPA

Values( @FinalID, GetDate(), @InAuditor, @Type, @Comments, @Hours,
@Expenses, @DinersExpenses )

if @@Error <> 0

BEGIN

ROLLBACK

Set @msg = 'The final timesheet you tried to save was already saved by
another user.'

END

else

BEGIN

COMMIT

Set @msg = 'The final timesheet has been saved.'

END

Select @msg


Nov 17 '05 #5

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

Similar topics

1
4777
by: Bill S. | last post by:
Hi, I a stored procedure that inserts a record into a table as below. The insert works OK, but if the insert violates a unique indewx constraint on one of the columns, the proc terminates immediately, and does NOT execute the 'if @@ERROR <> 0' statement.
2
5128
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data store. I've done a fair amount of research on concurrency handling in newsgroups and other resources. Below is what I've come up as a standard for handling concurrency thru stored procedures. I am sharing with everyone so I can get some comments...
9
10303
by: dtwilliams | last post by:
OK, i'm trying to do some error checking on stored procedures and am following the advise in Erland Sommarskog's 'Implementing Error Handling with Stored Procedures' document. Can anybody help with my stored procedures and why it keeps erroring at the '-- Create new Address Detail stage'? The errorCode value that is being return in my web app is 0, so i'm not even sure why it's even raising the error!! Rather than executing the INSERT...
0
4281
by: Rhino | last post by:
I've written several Java stored procedures now (DB2 V7.2) and I'd like to write down a few "best practices" for reference so that I will have them handy for future development. Would the experts here agree with the following? Would they add any other points? 1. If the shop standard calls for logging of application errors, a stored procedure should log any error that it encounters immediately upon detecting it and then return to the...
5
2280
by: Jurgen Defurne | last post by:
I am currently designing an application which should be accessible from different interfaces. For this I like to be using stored procedures to process the contents of form submissions and dialog screens. After studying the PG database, it seems to me that I can fulfill the following requirements. a) The basic contents of the internal data dictionary can be used to check incoming fields from on their length and permitted contents.
5
1739
by: csgraham74 | last post by:
Hi guys, Basically i have been developing in dotnet for a couple of years but ive had a few issues in regards to error handling. For example - I have a class that i call passing in a stored procedure and connection string as a path. My method returns a dataset. In my SP i have an output parameter which tells me whether the SP select is successful or not. If i get a error code passed back then i throw an exception this then returns...
1
2256
by: pob | last post by:
>From a form I have some code that calls 4 modules frmMain 1 mod 2 mod 3 mod 4 mod If mod 1 experiences an error the error handling works fine within mod 1 and writes out the error to a table, but the other modules still get
1
2493
by: | last post by:
I have an application that has a presentation later, business layer, and data layer. All three projects have their own exception policy, the "UI Policy", "BL Policy", "DL Policy", all of which will log the error in the application event logs. When a database error occurs such as a missing stored procedure, all three policies will log the event resulting in three different entries in the error. My boss says there is a way in the...
4
5079
by: barmatt80 | last post by:
I am stumped on the error reporting with sql server. I was told i need to return @SQLCode(code showing if successful or not) and @ErrMsg(and the message returned). I am clueless on this. I wrote this procedure: ALTER PROCEDURE . @Emp_SSN int, @Annual_Forward decimal(10,2),
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.