Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

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

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
conrad2010
Asked:
conrad2010
  • 2
  • 2
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
conrad2010Author Commented:
try catch sounds good, I'd like to only catch this specific error
0
 
rawinnlnx9Commented:
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
 
conrad2010Author Commented:
that's it...
0
 
Carl TawnSystems and Integration DeveloperCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now