How we hand sql exceptions? What is the class that handles SqlServer exceptions?

Showing Answers 1 - 1 of 1 Answers

behnam

  • Nov 13th, 2005
 

SqlExceptionclass is created whenever the .NET Framework Data Provider for SQL Server encounters an error generated from the server. (Client side errors are thrown as standard common language runtime exceptions.) SqlException always contains at least one instance of SqlError.

   ...

   try
   {
      myCommand.Connection.Open();
   }
   catch (SqlException e)
   {
     string errorMessages = "";

     for (int i=0; i < e.Errors.Count; i++)
     {
         errorMessages += "Index #" + i + "\n" +
                          "Message: " + e.Errors[i].Message + "\n" +
                          "LineNumber: " + e.Errors[i].LineNumber + "\n" +
                          "Source: " + e.Errors[i].Source + "\n" +
                          "Procedure: " + e.Errors[i].Procedure + "\n";
     }

     System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
     log.Source = "My Application";
     log.WriteEntry(errorMessages);
     Console.WriteLine("An exception occurred. Please contact your system administrator.");
   }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions