ToString Method
Collapse the table of content
Expand the table of content

SqlError.ToString Method ()

 

Gets the complete text of the error message.

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

public override string ToString()

Return Value

Type: System.String

The complete text of the error.

The string is in the form "SqlError:", followed by the Message, and the stack trace. For example:

SqlError:UserId or Password not valid. <stack trace>

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();
}

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft