Solved

Help with Exceptions

Posted on 2008-06-12
4
153 Views
Last Modified: 2013-11-26
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
Comment
Question by:R8VI
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 21767822
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 21767830
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question