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 ReadOnly Property LineNumber As Integer

Property Value

Type: System.Int32

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 Sub ShowSqlException(ByVal connectionString As String)
    Dim queryString As String = "EXECUTE NonExistantStoredProcedure"
    Dim errorMessages As New StringBuilder()

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)

        Try
            command.Connection.Open()
            command.ExecuteNonQuery()

        Catch ex As SqlException
            Dim i As Integer
            For i = 0 To ex.Errors.Count - 1
                errorMessages.Append("Index #" & i.ToString() & ControlChars.NewLine _
                    & "Message: " & ex.Errors(i).Message & ControlChars.NewLine _
                    & "Error Number: " & ex.Errors(i).Number & ControlChars.NewLine _
                    & "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _
                    & "Source: " & ex.Errors(i).Source & ControlChars.NewLine _
                    & "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine)
            Next i
            Console.WriteLine(errorMessages.ToString())
        End Try
    End Using
End Sub

.NET Framework
Available since 1.1
Return to top
Show: