This documentation is archived and is not being maintained.

SqlException.Class Property

Gets the severity level of the error returned from the .NET Framework Data Provider for SQL Server.

[Visual Basic]
Public ReadOnly Property Class As Byte
public byte Class {get;}
public: __property unsigned char get_Class();
public function get Class() : Byte;

Property Value

A value from 1 to 25 that indicates the severity level of the error.


Messages with a severity level of 10 or less are informational and indicate problems caused by mistakes in information that a user has entered. Severity levels from 11 through 16 are generated by the user, and can be corrected by the user. Severity levels from 17 through 25 indicate software or hardware errors. When a level 17, 18, or 19 error occurs, you can continue working, although you might not be able to execute a particular statement.

The SqlConnection remains open when the severity level is 19 or less. When the severity level is 20 or greater, the server usually closes the SqlConnection. However, the user can reopen the connection and continue. In both cases, a SqlException is generated by the method executing the command.

For information on the warning and informational messages sent by SQL Server, see the Troubleshooting section of the SQL Server documentation.

This is a wrapper for the Class property of the first SqlError in the Errors property.


[Visual Basic, C#, C++] The following example displays each SqlError within the SqlErrorCollection collection.

[Visual Basic] 
Public Sub ShowSqlException()
     Dim mySelectQuery As String = "SELECT column1 FROM table1"
     Dim myConnection As New SqlConnection ("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Sample;")
     Dim myCommand As New SqlCommand(mySelectQuery, myConnection)

     Catch e As SqlException
         Dim errorMessage As String = "Message: " & e.Message & vbCrLf & _
                                      "Source: " & e.Source & vbCrLf & _
                                      "State: " & e.State & vbCrLf & _
                                      "Procedure: " & e.Procedure & vbCrLf

        Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
        log.Source = "My Application"
        Console.WriteLine("An exception occurred. Please contact your system administrator.")
     End Try
End Sub

public void ShowSqlException() 
    string mySelectQuery = "SELECT column1 FROM table1";
    SqlConnection myConnection =
       new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Sample;");
    SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);

    catch (SqlException e) 
      string errorMessage = "Message: " + e.Message + "\n" +
                            "Source: " + e.Source + "\n" +
                            "State: " + e.State + "\n" +
                            "Procedure: " + e.Procedure + "\n";

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

void ShowSqlException() 
    String* mySelectQuery = S"SELECT column1 FROM table1";
    SqlConnection* myConnection =
       new SqlConnection(S"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Sample;");
    SqlCommand* myCommand = new SqlCommand(mySelectQuery,myConnection);

    catch (SqlException* e) 
      String* errorMessage = String::Concat(
         String::Format( S"Message: {0}\nSource: {1}\n", e->Message, e->Source ),
         String::Format( S"State: {0}\nProcedure: {1}\n", __box(e->State), e->Procedure ));

      System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
      log->Source = S"My Application";
      Console::WriteLine(S"An exception occurred. Please contact your system administrator.");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

SqlException Class | SqlException Members | System.Data.SqlClient Namespace | Number | Source | State | Server | Message | Procedure | LineNumber