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());
}
}
}