[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2010-08-12
6
Medium Priority
?
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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