Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

17 Experts available now in Live!

Get 1:1 Help Now