?
Solved

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

Posted on 2010-08-12
6
Medium Priority
?
679 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

801 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