mrkd1991
mrkd1991

Reputation: 398

Custom Exception Messages C#

How do I go about setting a custom error message based on which error is presented?

        protected void btnNew_Click(object sender, EventArgs e)
    {
        Clear();
        SqlCommand command = conn.CreateCommand();
        SqlDataReader reader;
        try
        {
            command.CommandText = "GetMax";
            command.CommandType = CommandType.StoredProcedure;
            conn.Open();
            reader = command.ExecuteReader();                
            while (reader.HasRows)
            {
                while (reader.Read())
                {
                    int CustMax = reader.GetInt32(0);
                    CustMax++;
                    txtCustID.Text = CustMax.ToString();
                }
                reader.NextResult();
            }
            reader.Dispose();
        }
        catch (SqlException ex)
        {
            lblMessage.Text = ex.Message;

        }
        finally
        {
            command.Dispose();
            conn.Dispose();
        }
    }

In this code block, if an SqlException is caught, it's message is displayed in lblMessage.

The main thing I am looking at is if there is a connection to the database or not. If there isn't, it results in the following error "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)".

If error 40 occurs, I want to set lblMessage to "Cannot connect to database", but if a different SqlException occurs, it should post the full error message.

Upvotes: 1

Views: 1942

Answers (2)

Kirk
Kirk

Reputation: 5077

Here is how you deal,

    catch (SqlException ex)
    {
        if(ex.Number == 40 ) // or Use whatever the number it returns for the relevant error
        {
             MessageBox.Show("Relevant SQL Error Occured");
        }
        else
        {
             MessageBox.Show("None Relevant SQL Error Occured:" + ex.toString());
        }
    }
    catch(Exception ex)// All other non sql errors
    {
        MessageBox.Show(ex.toString());
    }

Take a look at this MSDN Error handling technique for more information Link

Upvotes: 3

Dnyanesh
Dnyanesh

Reputation: 2343

You can modify your code like

catch (SqlException ex)
 {
    if(ex.Number == 40) // 40 is specific key for network issue
    {
      lblMessage.Text = "Cannot connect to database"  
    }
   else
   { 
    lblMessage.Text = ex.Message;
   }
 }

There is another way to achieve same thing like

catch ((SqlException ex) {
if (ex.Message.Contains("A network-related or instance-specific error specific"))//Put the matching text here  
{
     lblMessage.Text = "Cannot connect to database" 
}
else
   { 
    lblMessage.Text = ex.Message;
   }
 }

But I will recommend you catching exception with error number because that will be give you clean code and exact error will get caught.

Upvotes: 2

Related Questions