Click here to Skip to main content
14,427,102 members
Rate this:
Please Sign up or sign in to 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:

<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 12:47pm
v4
Comments
Wonde Tadesse 16-Dec-11 17:08pm
   
Can you elaborate what exactly the error message you received?
Rate this:
Please Sign up or sign in to vote.

Solution 1

There are quite a few things you can do.

1> In your code use try..catch..finally block.
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.


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>
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>
   
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"
Tech Code Freak 17-Dec-11 23:10pm
   
5up!Great Answer!
Rate this:
Please Sign up or sign in to vote.

Solution 2

Refer to this CP article, Error Handling in ASP.NET[^] and MSDN[^]
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100