Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi everybody,

I want to handle Errors in my ASP.Net application,

Example: When try to access Database and the connection is failed, i want to go to Error Page,

I try to use web config file like that:

HTML
<customErrors mode="On" defaultRedirect="ErrorPage.aspx" />


but it not help, and the error is rasied by Visual Studio itself,

I want to see my customErrorPage.aspx is displayed when failed to connect ot database,
I tried to use try/catch block and using statement, but with all cases that
it still display either The Exception Details Yellow Screen of Death error page or The Runtime Error Yellow Screen of Death error page,
I want to know why customErrorPage.aspx not displayed

Thanks
Posted
Updated 17-Dec-11 11:47am
v4
Comments
Wonde Tadesse 16-Dec-11 17:08pm    
Can you elaborate what exactly the error message you received?

There are quite a few things you can do.

1> In your code use try..catch..finally block.
C#
pulic void someFunction()
{

         try
            {
                //your business logic here
            }
         catch(Exception ex)
         {
             // Log your exception. you can use Log4Net
             throw ex;
          }
        finally
           {
              // Close your database connection here.
            }
}


2> In your Global.asax file you can log unhandled exception. I have used Log4Net Logging library.


C#
protected void Application_Error(object sender, EventArgs e)
       {
           System.Web.HttpContext context = HttpContext.Current;
           Exception ex = context.Server.GetLastError().GetBaseException();


           if (!ex.Message.Contains("Invalid length for a Base-64 char array")) // Remove invalid exception occuring in the system when user cancels the postback.
           {
               if (!(context.Request.FilePath.ToString().ToUpper().Contains(("ScriptResource.axd").ToUpper()) || context.Request.FilePath.ToString().ToUpper().Contains(("WebResource.axd").ToUpper())))
               {

                   CLogger.WriteLog(ELogLevel.ERROR, "URL : " + context.Request.Url.ToString() + "\n");

                   CLogger.WriteLog(ELogLevel.ERROR, "FILE : " + context.Request.FilePath.ToString() + "\n");

                   CLogger.WriteLog(ELogLevel.ERROR, "USER : " + context.User.Identity.Name + "\n");

                   CLogger.WriteLog(ELogLevel.ERROR, "MESSAGE : " + ex.Message + "\n");
                   CLogger.WriteLog(ELogLevel.ERROR, "SOURCE : " + ex.Source + "\n");
                   CLogger.WriteLog(ELogLevel.ERROR, "STACK TRACE : " + ex.StackTrace + "\n");
                   CLogger.WriteLog(ELogLevel.ERROR, "INNER EXCEPTION : " + ex.InnerException + "\n");
                   CLogger.WriteLog(ELogLevel.ERROR, "---------------------------------------------------------------------------------------------\n");
               }
           }





       }



for details check Clickty [^]

3> For database specific connection handling use "using statement".


4>
XML
Two things : in your web.config add

<customerrors mode="On" defaultredirect="~/Error.aspx" redirectmode="ResponseRedirect"></customerrors>

and set

<compilation debug="false">

in your production version to redirect users to error page.</compilation>
 
Share this answer
 
v2
Comments
thatraja 16-Dec-11 22:46pm    
5!
RempoRaaj 16-Dec-11 23:48pm    
Good answer.....
MrLonely_2 17-Dec-11 17:46pm    
Thank you very much mr.virang_21, but i want only something that:
I want to see my customErrorPage.aspx is displayed when failed to connect ot database,
I tried to use try/catch block and using statement, but with all cases that
but it still display either The Exception Details Yellow Screen of Death error page or The Runtime Error Yellow Screen of Death error page,
I want to know why customErrorPage.aspx not displayed
Thanks again
virang_21 17-Dec-11 17:56pm    
Check point 4 of the solution.
MrLonely_2 17-Dec-11 18:19pm    
It give me error that:
Unrecognized attribute 'redirectMode'. Note that attribute names are case-sensitive.
I write it as : redirectMode="ResponseRedirect"
Refer to this CP article, Error Handling in ASP.NET[^] and MSDN[^]
 
Share this answer
 
v2
Comments
thatraja 16-Dec-11 22:46pm    
5!
Nikil S 17-Dec-11 6:14am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900