Solved

ASP.NET C# Hot to trap a System.Data.SqlClient.SqlException error

Posted on 2011-02-17
5
592 Views
Last Modified: 2012-05-11
On my asp.net C# web page, how can I trap this specific error and redirect to a custom error page?

System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server.
0
Comment
Question by:conrad2010
  • 2
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34919477
How are you connecting to your database? If it's through code then it's easy enough by simply wrapping your database code in a try...catch block. If you are using a SqlDataSource component then it might be a little more fiddly.
0
 

Author Comment

by:conrad2010
ID: 34919518
try catch sounds good, I'd like to only catch this specific error
0
 
LVL 9

Accepted Solution

by:
rawinnlnx9 earned 500 total points
ID: 34919522
http://msdn.microsoft.com/en-us/library/aa256813(v=sql.80).aspx

The above provides a pretty good discussion that should get you started.
0
 

Author Closing Comment

by:conrad2010
ID: 34919531
that's it...
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34919570
Well, that error is reported as a SqlException along with all other Sql errors. If you want to isolate that single error itself then you would have to examine the details of the exception:
SqlConnection conn = new SqlConnection("Your connection string");

try
{
     conn.Open();
}
catch (SqlException ex)
{
                if (ex.Number == 53)
                {
                    // redirect
                }
}
finally
{
     conn.Close();
}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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