Solved

Help with Exceptions

Posted on 2008-06-12
4
148 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now