Export (0) Print
Expand All

SqlException.LineNumber Property

Gets the line number within the Transact-SQL command batch or stored procedure that generated the error.

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

public int LineNumber { get; 
/** @property */
public int get_LineNumber ()

public function get LineNumber () : int

Property Value

The line number within the Transact-SQL command batch or stored procedure that generated the error.

The line numbering starts at 1; if 0 is returned, the line number is not applicable.

This is a wrapper for the LineNumber property of the first SqlError in the Errors property.

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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft