views:

547

answers:

3

How to setup a common error page in ASP.NET website? Also how to handel the error in Data Access Layer by common error page?

+2  A: 

Check this article :

Rich Custom Error Handling with ASP.NET

An advanced one with HttpModules :

ELMAH

Canavar
I would also add that for non-ASP.NET requests (images, js, css etc) that could produce a 404, you need to ensure that IIS is configured to use the same error handler, otherwise you'll see the standard windows Page Not Found message.See for details: http://stackoverflow.com/questions/497735/
Zhaph - Ben Duguid
+1  A: 

In my current project I'm sticking with Application_Error in the global.asax for showing the end-users a uniform error page in case of any unhandled errors. I added a sendmail call to mail certain exceptions to a mail address to get a better insight in what went wrong (you can't rely on customers/visitors to properly describe the problem). After sending the mail and/or logging the problem I redirect users to a error.html with a generic errormessage.

In my DAL I try/catch most of the critical functions and show a warning/error accordingly (I return a status/message to show if for example a connection couldn't be made/timed out).

Shogun
+1  A: 

This is the pattern I tend to start with when starting a new app. Apologies this is in VB.NET ;)

In the global.asax Server.Transfer to your custom Error page.

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

        Server.Transfer("~/Error.aspx", False)

    End Sub

Then in your custom error page.

    Private Sub Page_Load

         Response.Clear() 
         Dim err As Exception = Server.GetLastError
         ...
    End Sub

Now you can test the Type of the exception. You'll need to recurse down through the inner exceptions as the parent exception will be probably be a general web exception. Get your DAL to throw a custom typed exception and you can test for that and handle differently.

cjuk