0 out of 3 rated this helpful - Rate this topic

SqlException.Number Property

Gets a number that identifies the type of error.

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)
public int Number { get; }

Property Value

Type: System.Int32
The number that identifies the type of error.

This is a wrapper for the Number property of the first SqlError in the Errors property. For more information on SQL Server engine errors, see SQL Server Books Online.

The following example displays each SqlError within the SqlErrorCollection collection.


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);
        try
        {
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            for (int i = 0; i < ex.Errors.Count; i++)
            {
                errorMessages.Append("Index #" + i + "\n" +
                    "Message: " + ex.Errors[i].Message + "\n" +
                    "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                    "Source: " + ex.Errors[i].Source + "\n" +
                    "Procedure: " + ex.Errors[i].Procedure + "\n");
            }
            Console.WriteLine(errorMessages.ToString());
        }
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Worthless....again
Please Remove the other comment's links, because they are broken.

Microsoft should be providing links to documentation for their own products (e.g. SQL Server). This should not be the responsibility of the user community.
List of SQL Server error numbers.
SELECT message_id, * FROM sys.messages
SQL Server Books Online link
The article doesn't provide a link to the SQL Server Books online errors list, so here you go:
http://msdn.microsoft.com/en-us/library/cc645603.aspx (SQL Server 2008 R2)
http://msdn.microsoft.com/en-us/library/cc645603(v=SQL.100).aspx (SQL Server 2008)
http://msdn.microsoft.com/en-us/library/ms365262(v=SQL.90).aspx (SQL Server 2005 - Find the Number in the left pane)

Be aware that even though the links provided were valid September 2011, they could change in the future.