Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
SqlException Class

SqlException Class

The exception that is thrown when SQL Server returns a warning or error. This class cannot be inherited.

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

type SqlException =  
        inherit DbException 

The SqlException type exposes the following members.

Public propertyClassGets the severity level of the error returned from the .NET Framework Data Provider for SQL Server.
Public propertyClientConnectionIdRepresents the client connection ID. For more information, see Data Tracing in ADO.NET.
Public propertyDataGets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.)
Public propertyErrorCodeGets the HRESULT of the error. (Inherited from ExternalException.)
Public propertyErrorsGets a collection of one or more SqlError objects that give detailed information about exceptions generated by the .NET Framework Data Provider for SQL Server.
Public propertyHelpLinkGets or sets a link to the help file associated with this exception. (Inherited from Exception.)
Public propertyHResultGets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.)
Public propertyInnerExceptionGets the Exception instance that caused the current exception. (Inherited from Exception.)
Public propertyLineNumberGets the line number within the Transact-SQL command batch or stored procedure that generated the error.
Public propertyMessageGets a message that describes the current exception. (Inherited from Exception.)
Public propertyNumberGets a number that identifies the type of error.
Public propertyProcedureGets the name of the stored procedure or remote procedure call (RPC) that generated the error.
Public propertyServerGets the name of the computer that is running an instance of SQL Server that generated the error.
Public propertySourceGets the name of the provider that generated the error. (Overrides Exception.Source.)
Public propertyStackTraceGets a string representation of the immediate frames on the call stack. (Inherited from Exception.)
Public propertyStateGets a numeric error code from SQL Server that represents an error, warning or "no data found" message. For more information about how to decode these values, see SQL Server Books Online.
Public propertyTargetSiteGets the method that throws the current exception. (Inherited from Exception.)

Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetBaseExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetObjectDataSets the SerializationInfo with information about the exception. (Overrides Exception.GetObjectData(SerializationInfo, StreamingContext).)
Public methodGetTypeGets the runtime type of the current instance. (Inherited from Exception.)
Public methodToStringReturns a string that represents the current SqlException object, and includes the client connection ID (for more information, see ClientConnectionId). (Overrides ExternalException.ToString().)

In .NET Framework Client Profile, this member is overridden by ToString().

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

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 SQL Server Books Online. The SqlException class maps to SQL Server severity.

The following is general information on handling exceptions. Your code should catch exceptions to prevent the application from crashing and to allow displaying a relevant error message to the user. You can use database transactions to ensure that the data is consistent regardless of what happens in the client application (including a crash). Features like System.Transaction.TransactionScope or the BeginTransaction method (in System.Data.OleDb.OleDbConnection, System.Data.ODBC.ODBCConnection, and System.Data.SqlClient.SqlConnection) ensure consistent data regardless of exceptions raised by a provider. Transactions can fail, so catch failures and retry the transaction.

The exception class of a .Net Framework data provider reports provider-specific errors. For example System.Data.Odbc has OdbcException, System.Data.OleDb has OleDbException, and System.Data.SqlClient has SqlException. For the best level of error detail, catch these exceptions and use the members of these exception classes to get details of the error.

In addition to the provider-specific errors, .NET Framework data provider types can raise .NET Framework exceptions such as System.OutOfMemoryException and System.Threading.ThreadAbortException. Recovery from these exceptions may not be possible.

Bad input can cause a .NET Framework data provider type to raise an exception such as System.ArgumentException or System.IndexOutOfRangeException. Calling a method at the wrong time can raise System.InvalidOperationException.

So, in general, write an exception handler that catches the provider specific exception as well as exceptions from the common language runtime. These can be layered as follows:

Try {
   // code here
catch (SqlException odbcEx) {
   // Handle more specific SqlException exception here.
catch (Exception ex) {
   // Handle generic ones here.


Try {
   // code here
catch (Exception ex) {
   if (ex is SqlException) {
      // Handle more specific SqlException exception here.
   Else {
      // Handle generic ones here.

It is also possible for a .NET Framework data provider method call to fail on a thread pool thread with no user code on the stack. In this case, and when using asynchronous method calls, you must register the UnhandledException event to handle those exceptions and avoid application crash.

The following example generates a SqlException and then displays the exception.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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