0

I am working in MVC3 Dot Net project Using EF.

We are catching all the Exceptions in an object of Exception (i.e ex) From this how can I get (or filter) SQlExceptions and I like to show the meaningful message.

facing problem : I am not getting System.Data.UpdateException class

2 Answers 2

1

loop through exception/inner excetion and check if(ex is SQlException) { } Maybe you missed adding reference to System.Data.Entity so you dont see System.Data.UpdateException class

Sign up to request clarification or add additional context in comments.

2 Comments

Thank u friend i got solution for System.Data.UpdateException
If the answer was the solution, please mark it as the definitive :)
1

You shouldn't be catching Exception first (or at all unless you are specifically doing something with all exceptions)

If you want only UpdateExceptions, then you should only catch that exception. This catch should be above any other handlings that are more generic, for example

try
{
}
catch(System.Data.UpdateException ex)
{

}
catch(Exception ex) //optionally 
{
///less generic handling
}

Also consider for general logging ELMAH http://code.google.com/p/elmah/

2 Comments

you appose is very nice but want if (e.Exception != null) { if (e.Exception is SqlException) { SqlException sqlExcep = (SqlException)e.Exception; switch (sqlExcep.Number) { case SqlExceptionNumber.ForeignKeyConstraint: this.ExceptionDetails.Text += "Error : Foreign Key Violation.The Contract cannot be inserted."; break; case SqlExceptionNumber.UniqueKeyConstraint:
Post some code above I'm not fully understanding. You want to check if e.Exception is of that type rather than a thrown exception?

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.