C# .NET - Catch MYSQL exception(Resource File)

Asked By Vasanth
09-Sep-10 05:17 AM
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
  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

}

  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:  http://msdn.microsoft.com/en-us/library/system.data.sqlclient.aspx
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 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlerror.aspx.
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
help
Frequently asked Interview Questions in ADO.Net hi friends Any one send frequently asked Important questions in C# .Net, ADO .Net, Asp .Net and Sql Server. . . . . . . . tx in Advance. . . . . . Hi, Find this. . (B)What is an IL? (B)What is a B) What is concept of Boxing and Unboxing ? (B) What is the difference between VB.NET and C#? (I) what is the difference between System exceptions and Application exceptions? (I)What is CODE Access security? (I)What is a satellite assembly? (A) How to prevent my .NET DLL to be decompiled? (I) what is the difference between Convert.toString and .toString () method
Interview Questions for .NET Framework This article is specially for the users those are in development or want to be a .net developer • To test a Web Service you must create a windows application or web application to consume this service? It is True / False? FALSE How many classes can a single.NET DLL contain? Answer1: As many Answer2: One or more What are good ADO.NET object(s) to replace the ADO Recordset object? The differences includes In ADO, the in memory representation of data is the recordset. In ADO.net, it is the dataset A recordset looks like a single table in ADO In contrast, a dataset is a collection of one or more tables in ADO.net ADO is designed primarily for connected access ADO.net the disconnected access to the database
choosen the Blog post from here This means the installation should be on a single server as Domain Controller , as MS SQL database server and as MOSS2007 server farm. Only one uses should be used. Thats what I have done, I followed the domain ist named SPTEST.INT. After the creation of the web applications, the shared service provider and finally the colaboration site as described in the howto, i like to login via I am reading the logfiles placed under "c: \ program files \ common files \ microsoft shared \ web server extensions \ 12 \ logs". I don't find any relavant information regarding authentication or what else 13:53:11.35 OWSTIMER.EXE (0x0980) 0x0988 Windows SharePoint Services Database 880i High System.Data.SqlClient.SqlException: Die von der Anmeldung angeforderte 'SharePoint_07_Config'-Datenbank kann nicht geöffnet werden. Fehler bei der Anmeldung. Fehler bei der Anmeldung für den Benutzer 'SPTEST \ spadmin'. bei System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) bei System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj