Solved

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

Posted on 2011-02-17
5
586 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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

20 Experts available now in Live!

Get 1:1 Help Now