Solved

ASP.net C# -- error handling when stored procedure is called

Posted on 2010-08-12
6
670 Views
Last Modified: 2012-05-10
We use SQL Server 2005 for db backen.
We use ASP.net 3.5 and C# for the front end.

Here in our shop I am being told that if I (the DBA) use TRY CATCH error handling in the stored procedure (that is called from the webpage) that C# error handling can't "see" the error, etc. and that I should use old style error handling by checking @@Error and letting C# do most of the rest.  I'm told that if I don't use TRY CATCH then C# can "see" and handle the errors thrown by the stored procedure.

This does not quite smell right to me as the DBA.  So, how should error handling be done when C# calls a stored procedure?  Can you point me to a resource online?  I need to know how best to do the error handling in the page itself and in the stored procedure.

Thanks!

Eric

PS:  I have googled a lot but not found an article that addresses how to handle errors in the SP when the SP is called from C#.
0
Comment
Question by:Eric3141
  • 2
  • 2
  • 2
6 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 33423052
You can do all the error handling you want in the stored procedure, but if you want to throw an error to the C# level, you can just use RAISERROR to accomplish that.
0
 
LVL 2

Author Comment

by:Eric3141
ID: 33423122
jensfiederer,

Thx for the feedback.  I have pasted the SP as it is right now.  This is how they have been coded in our shop to be used by ASP.net.  Here I have coded it to write 2 entries to the db error log file.  Which of the entries will ASP.net be able to "see" / handle?  The last one?

Is there any particular advantage to leaving it as it's coded here?  The programmer who used to be here seemed to think so, however, I am skeptical.
ALTER PROCEDURE [dbo].[uspCaseActionHistoryDelete]
	--input parameters
	@Original_HistoryID int

AS
BEGIN
	SET NOCOUNT OFF;

	BEGIN TRANSACTION DeleteTrans

		DELETE FROM History
		WHERE HistoryID = @Original_HistoryID;
		
		IF @@Error != 0
			-- Whoops, there was an error:
			BEGIN
				ROLLBACK TRANSACTION DeleteTrans
				 
				RAISERROR ('CaseMgt DB - stored proc = uspCaseActionHistoryDelete',16,10)WITH LOG
				RAISERROR ('Delete from CaseActionHistory table failed',16,10)WITH LOG

				RETURN
			END

	--If we reach here, success!
	COMMIT TRANSACTION DeleteTrans

END

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33423397
In that specific scenario your C# app would receive both errors concatenated into a single exception.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 2

Author Comment

by:Eric3141
ID: 33423959
OK, do either of you know of an example (or a location on the web with one) of how to use what RAISERROR sends from the SP to ASP?

We execute SP via ObjectDataSource -- do I check for the error (from RAISERROR) in the ODS_INSERTED event handler?  

I'm sorry, but I have no idea how to do this -- any resources online you can point me to?
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 33424223
Never tried this with an OBJECT datasource  (are you sure it's not SqlDataSource? ) - but if the code that IMPLEMENTS that object  executes any commands, it might look like this:

            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ToString()))
            {
                con.Open();

                try
                {
                    SqlCommand c = con.CreateCommand();
                    c.CommandType = System.Data.CommandType.StoredProcedure;
                    c.CommandText = "jens_foo";
                    c.ExecuteNonQuery();
                }
                catch (SqlException exc)
                {
                    Console.WriteLine(exc.Message);
                }
            }
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 33424245
You will need to use the event handler like you said.

The eventargs object passed to the Inserted event handler will contain an Exception object, you simply need to test if this is null:


        protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if (e.Exception != null)
            {
                // do something with the exception
            }
        }

Open in new window

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question