Solved

Help with Exceptions

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

9 Experts available now in Live!

Get 1:1 Help Now