Catch MYSQL exception(Resource File)

Asked By Vasanth
09-Sep-10 05:17 AM
Earn up to 0 extra points for answering this tough question.
Hi,
I have to catch system exceptions and Sql exceptions and have to display my own exceptions to users.  Instead of using a table to store error message and error id, have to use Resourse file in my windows application. Can anyone give an idea to do this.

Sql exception number     My exception number my message to user
411 1           Invalid Value
415 2           Empty values not   allowed
417 3        DB not connected

  re: Catch MYSQL exception(Resource File)

Sagar P replied to Vasanth
09-Sep-10 05:31 AM
You can use SqlException class for that;

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception(VS.71).aspx

Like this;

try

{

  //Sql statements

}

catch (SqlException sqlEx)

{

  //Display user defined error message here

}

catch (Exception ex)

{

  //Catch other than sql exceptions here

}

  re: Catch MYSQL exception(Resource File)

Reena Jain replied to Vasanth
09-Sep-10 05:48 AM
hi,
SqlException Class
here is some more information for you

The exception that is thrown when SQL Server returns a warning or error. This class cannot be inherited.

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

This class 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.
Copy
 
Try {
   // code here
}
catch (SqlException odbcEx) {
   // Handle more specific SqlException exception here.
}
catch (Exception ex) {
   // Handle generic ones here.
}
or

Try {
   // code here
}
catch (Exception ex) {
   if (ex is SqlException) {
    // Handle more specific SqlException exception here.
   }
   Else {
    // Handle generic ones here.
   }
}
eg
public static void ShowSqlException(string connectionString)
{
  string queryString = "EXECUTE NonExistantStoredProcedure";
  StringBuilder errorMessages = new StringBuilder();
 
  using (SqlConnection connection = new SqlConnection(connectionString))
  {
    SqlCommand command = new SqlCommand(queryString, connection);
    try
    {
      command.Connection.Open();
      command.ExecuteNonQuery();
    }
    catch (SqlException ex)
    {
      for (int i = 0; i < ex.Errors.Count; i++)
      {
        errorMessages.Append("Index #" + i + "\n" +
          "Message: " + ex.Errors[i].Message + "\n" +
          "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
          "Source: " + ex.Errors[i].Source + "\n" +
          "Procedure: " + ex.Errors[i].Procedure + "\n");
      }
      Console.WriteLine(errorMessages.ToString());
    }
  }
}
Create New Account