This documentation is archived and is not being maintained.

SqlException.Number Property

Gets a number that identifies the type of error.

[Visual Basic]
Public ReadOnly Property Number As Integer
public int Number {get;}
public: __property int get_Number();
public function get Number() : int;

Property Value

The number that identifies the type of error.


This number corresponds to an entry in the master.dbo.sysmessages table.

This is a wrapper for the Number 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 | State | Class | Source | Server | Message | Procedure | LineNumber