• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

Help with Exceptions

Hi,

I want to throw a freindly message.
What i am trying to do is that connect to a database I have done this but I want to throw a friendly error message if web page doesnt manage to connect to the database at any stage.

Is this possible and how ?

Thanks,

R8VI
0
R8VI
Asked:
R8VI
  • 2
1 Solution
 
DhaestCommented:
Just use try-catch statement
try
{
'  open your connection
' performs some sql
}
catch(Exception x)
{

}

Some references
http://www.c-sharpcorner.com/UploadFile/rajeshvs/ExceptionHandlinginCSharp11282005051444AM/ExceptionHandlinginCSharp.aspx
http://msdn.microsoft.com/en-us/library/0yd65esw(VS.71).aspx
try
{
// Statement which can cause an exception.
}
catch(Type x)
{
// Statements for handling the exception
}
finally
{
//Any cleanup code
} 

Open in new window

0
 
DhaestCommented:
SqlException-class

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception.aspx#Mtps_DropDownFilterText
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());
        }
    }
}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now