1

So, I am getting an exception in Stored Procedure whose error code is 547. I have created a data diagram in sql server, where I have defined the relationships.when I run the any delete SP I get an error saying FK conflict, which is right. My problem is how do I get the number(i.e 547) in my C# code.

catch (Exception ex)
        {

            lblMessage.Text = ex.Message;
        }

I sthere any way where I can get this 547 code in my C# code.? Like

 ex.somethin (which gives me the error message's code).

4 Answers 4

4

Try This.

    try
    {

    }
    catch(SqlException ex)
    {
        lblMessage.Text = ex.Message;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

but what if I get an exception other than sql exception.? Can I not use Exception and still get error code.?
2

Multiple catches can be used:

 try
        {

        }
      catch(SqlException sqlex)
        {
          if(sqlex.Number ==547)
               {
                   //code
               }
        }
        catch(Exception ex)
        {
            lblMessage.Text = ex.Message;
        }

Comments

1

You can try using Elmah library (Error Logging Modules And Handlers)

Here is a step by step tutorial on how to use it: http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/logging-error-details-with-elmah-cs

1 Comment

Related to this: Take a look at bitbucket.org/wasp/elmahr/wiki/Home. This is a dashboard to observe errors in your applications in realtime and does not require you to implement more then Elmah into your application. Comes in real handy!
1

For more details http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception.aspx

 try
 {
   ... 
   ...
 }
 catch(SqlException ex)
 {
    lblMessage.Text = ex.Message;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.