0

I want to catch specific SqlExceptions in my code. Suppose If I want to check if the Exception is caused due to some connection failure then I want to send a message to the client DB Connectivity issue. Is there a way I can check it programmatically?

public void GetData()
{
    try
    {
        using (con)
        {
            ....
            con.Open();
            cmd.ExecuteNonQuery();
        }
    }
    catch (SqlException ex)
    {
        System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
        //if(ex == sqlconnectionexception)
        throw new FaultException(new FaultReason(new FaultReasonText("DB Connectivity issue")));
    }  
} 
Multi stack
  • 311
  • 3
  • 9
  • 18
  • you can check for error codes. (Number) – Shaminder Singh Feb 17 '16 at 12:18
  • 1
    `SqlException.Number` returns the error code of the exception. You can run a `switch-case` on that and handle different error codes however you want to. – Oliver Nicholls Feb 17 '16 at 12:18
  • 1
    https://msdn.microsoft.com/en-us/library/cc645603.aspx Heres a list of all the error codes - though there are a HUGE number – Takarii Feb 17 '16 at 12:20
  • Thank you all! Went through this http://stackoverflow.com/questions/4825365/how-to-know-actual-problem-because-of-which-sqlexception-is-thrown and it helped me. But Im wondering is there any easy way to find out these `Number`? Like I want to know the `Number` for `duplicate key row` exception on insert. Any short hand easy way to find it out? – Multi stack Feb 17 '16 at 12:40

0 Answers0