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

The following table describes the possible values for this property:

Source of Error

SqlError.Number

SqlError.State

SqlException has inner Win32Exception (beginning with.NET Framework 4.5)

Error from server

Server error code

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

Typically greater than 0

No

Connection timeout

-2

0

Yes (Number = 258)

Communication error (non-LocalDB)

Win32 error code

0

Yes (Number = Win32 error code)

Communication error (LocalDB)

Win32 error code

0

No

Encryption capability mismatch

20

0

No

Failed to start LocalDB

Win32 error code

0

No

Read-only routing failure

0

0

No

Server had severe error processing query

0

0

No

Processed cancellation while parsing results

0

0

No

Failed to create user instance

0

0

No

For more information on errors generated by SQL Server, see SQL Server Books Online.

The following example displays each SqlError within the SqlErrorCollection collection.

public void DisplaySqlErrors(SqlException exception)
{
	for (int i = 0; i < exception.Errors.Count; i++)
	{
		Console.WriteLine("Index #" + i + "\n" +
			"Source: " + exception.Errors[i].Source + "\n" +
			"Number: " + exception.Errors[i].Number.ToString() + "\n" +
			"State: " + exception.Errors[i].State.ToString() + "\n" +
			"Class: " + exception.Errors[i].Class.ToString() + "\n" +
			"Server: " + exception.Errors[i].Server + "\n" +
			"Message: " + exception.Errors[i].Message + "\n" +
			"Procedure: " + exception.Errors[i].Procedure + "\n" +
			"LineNumber: " + exception.Errors[i].LineNumber.ToString());
	}
	Console.ReadLine();
}

.NET Framework
Available since 1.1
Return to top
Show: