Solved

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

Posted on 2010-08-12
6
672 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help in debugging a UDF results 7 51
orderby list (from Json) 1 28
Calculating percentage 2 28
c#, case, if 4 17
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

763 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