SqlErrorCollection.Item Property (Int32)

 

Gets the error at the specified index.

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

public SqlError this[
	int index
] { get; }

Parameters

index
Type: System.Int32

The zero-based index of the error to retrieve.

Property Value

Type: System.Data.SqlClient.SqlError

A SqlError that contains the error at the specified index.

Exception Condition
IndexOutOfRangeException

Index parameter is outside array bounds.

The following example displays each SqlError within the SqlErrorCollection collection.

public static void ShowSqlException(string connectionString)
{
    string queryString = "EXECUTE NonExistantStoredProcedure";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        try
        {
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            DisplaySqlErrors(ex);
        }
    }
}

private static void DisplaySqlErrors(SqlException exception)
{
    for (int i = 0; i < exception.Errors.Count; i++)
    {
        Console.WriteLine("Index #" + i + "\n" +
            "Error: " + exception.Errors[i].ToString() + "\n");
    }
    Console.ReadLine();
}

.NET Framework
Available since 1.1
Return to top
Show: