SqlException.Class Property


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

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

public byte Class { get; }

Property Value

Type: System.Byte

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

Messages that have 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 ordinarily 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 about 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.

The following example displays each SqlError within the SqlErrorCollection collection.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

class Program
    static void Main()
        string s = GetConnectionString();

    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);
            catch (SqlException ex)
                for (int i = 0; i < ex.Errors.Count; i++)
                    errorMessages.Append("Index #" + i + "\n" +
                        "Message: " + ex.Errors[i].Message + "\n" +
                        "Error Number: " + ex.Errors[i].Number + "\n" +
                        "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                        "Source: " + ex.Errors[i].Source + "\n" +
                        "Procedure: " + ex.Errors[i].Procedure + "\n");

    static private string GetConnectionString()
        // To avoid storing the connection string in your code, 
        // you can retrieve it from a configuration file.
        return "Data Source=(local);Initial Catalog=AdventureWorks;"
            + "Integrated Security=SSPI";

.NET Framework
Available since 1.1
Return to top