SqlException.Number Property
.NET Framework 4
Gets a number that identifies the type of error.
Assembly: System.Data (in System.Data.dll)
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()); } } }
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.
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.
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.
- 5/15/2012
- r.y.a.n._
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.
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.
- 9/2/2011
- Mr. Andersen