This documentation is archived and is not being maintained.

SqlError Class

Collects information relevant to a warning or error returned by SQL Server. This class cannot be inherited.

For a list of all members of this type, see SqlError Members.


[Visual Basic]
NotInheritable Public Class SqlError
public sealed class SqlError
public __gc __sealed class SqlError
class SqlError

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


This class is created by the .NET Framework Data Provider for SQL Server when an error occurs. An instance of SqlError is created and managed by the SqlErrorCollection, which in turn is created by the SqlException class.

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.


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

[Visual Basic] 
Public Sub DisplaySqlErrors(myException As SqlException)
    Dim i As Integer
    For i = 0 To myException.Errors.Count - 1
        MessageBox.Show("Index #" & i & ControlChars.NewLine & _
                        "Error: " & myException.Errors(i).ToString() & ControlChars.NewLine)
    Next i
End Sub 'DisplaySqlErrors

public void DisplaySqlErrors(SqlException myException) 
    for (int i=0; i < myException.Errors.Count; i++)
       MessageBox.Show("Index #" + i + "\n" +
            "Error: " + myException.Errors[i].ToString() + "\n");

void DisplaySqlErrors(SqlException* myException) 
    for (int i=0; i < myException->Errors->Count; i++)
       MessageBox::Show(String::Format( S"Index #{0}\nError: {1}\n",
          __box(i), myException->Errors->Item[i] ));

[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.


Namespace: System.Data.SqlClient

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

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

See Also

SqlError Members | System.Data.SqlClient Namespace | SqlErrorCollection | SqlException