tia_kamakshi
asked on
Exception handling C#
Hi,
I am working on C#, ASP.net 2.0
In my global.aspx and on Application_Error,
I get the exception
Exception handledException = Server.GetLastError();
HttpContext.Current.Sessio n.Add("Las tError", handledException);
but at line
HttpContext.Current.Sessio n.Add("Las tError", handledException);
it says
An exception of type 'System.NullReferenceExcep tion' occurred in App_global.asax.lp6-olqc.d ll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
when the actual exception details are as below, says file does not exists
{System.Web.HttpException: The file '/Studio/VW/MainScreens/Ho meP098098a ge.aspx' does not exist.
at System.Web.UI.Util.CheckVi rtualFileE xists(Virt ualPath virtualPath)
at System.Web.Compilation.Bui ldManager. GetVPathBu ildResultI nternal(Vi rtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
at System.Web.Compilation.Bui ldManager. GetVPathBu ildResultW ithNoAsser t(HttpCont ext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
at System.Web.Compilation.Bui ldManager. GetVirtual PathObject Factory(Vi rtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
at System.Web.Compilation.Bui ldManager. CreateInst anceFromVi rtualPath( VirtualPat h virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
at System.Web.UI.PageHandlerF actory.Get HandlerHel per(HttpCo ntext context, String requestType, VirtualPath virtualPath, String physicalPath)
at System.Web.UI.PageHandlerF actory.Sys tem.Web.IH ttpHandler Factory2.G etHandler( HttpContex t context, String requestType, VirtualPath virtualPath, String physicalPath)
at System.Web.HttpApplication .MapHttpHa ndler(Http Context context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
at System.Web.HttpApplication .MapHandle rExecution Step.Syste m.Web.Http Applicatio n.IExecuti onStep.Exe cute()
at System.Web.HttpApplication .ExecuteSt ep(IExecut ionStep step, Boolean& completedSynchronously)}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
HttpContext.Current.Respon se.ClearCo ntent();
Exception handledException = Server.GetLastError();
HttpContext.Current.Sessio n.Add("Las tError", handledException);
string applPath = Request.ApplicationPath;
Server.Transfer(applPath + "/errorTemplate.aspx", true);
}
But if some the error thrown by application by like Index outof bound then it works fine and my details are redirected to
Server.Transfer(applPath + "/errorTemplate.aspx", true);
but if file not found then it is not
Can anyone help me on this.
Thanks
I am working on C#, ASP.net 2.0
In my global.aspx and on Application_Error,
I get the exception
Exception handledException = Server.GetLastError();
HttpContext.Current.Sessio
but at line
HttpContext.Current.Sessio
it says
An exception of type 'System.NullReferenceExcep
Additional information: Object reference not set to an instance of an object.
when the actual exception details are as below, says file does not exists
{System.Web.HttpException:
at System.Web.UI.Util.CheckVi
at System.Web.Compilation.Bui
at System.Web.Compilation.Bui
at System.Web.Compilation.Bui
at System.Web.Compilation.Bui
at System.Web.UI.PageHandlerF
at System.Web.UI.PageHandlerF
at System.Web.HttpApplication
at System.Web.HttpApplication
at System.Web.HttpApplication
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
HttpContext.Current.Respon
Exception handledException = Server.GetLastError();
HttpContext.Current.Sessio
string applPath = Request.ApplicationPath;
Server.Transfer(applPath + "/errorTemplate.aspx", true);
}
But if some the error thrown by application by like Index outof bound then it works fine and my details are redirected to
Server.Transfer(applPath + "/errorTemplate.aspx", true);
but if file not found then it is not
Can anyone help me on this.
Thanks
ASKER
Yes, you are right the session is null, but why this is happening?
As user is logged in and we have the user information in session. When I am knowlingly giving wrong name to the page in URL
why in application_error method of global.asax says session is null.
But thereafter without doing anything If i go to the correct logged in page I can see loggedin user name
Please help me in this
Thanks
As user is logged in and we have the user information in session. When I am knowlingly giving wrong name to the page in URL
why in application_error method of global.asax says session is null.
But thereafter without doing anything If i go to the correct logged in page I can see loggedin user name
Please help me in this
Thanks
The check for the existance of the page is done before the cookies are examined to match the user with a session.
You can specify that a page should be session-less, so the page has to be parsed before the server even knows if sessions should be used or not.
You can specify that a page should be session-less, so the page has to be parsed before the server even knows if sessions should be used or not.
ASKER
Thanks for the details.
So what is the best way to transfer the details to my errorTemplate.aspx file, so that I can show user the error details in my aspx file.
If I create a new Session object in the else part, than I may loose my information stored in the session like logged in user information
if (session != null)
{
HttpContext.Current.Sessio n.Add("Las tError", handledException);
}
else
{
}
string applPath = Request.ApplicationPath;
Server.Transfer(applPath + "/errorTemplate.aspx", true);
Thanks
So what is the best way to transfer the details to my errorTemplate.aspx file, so that I can show user the error details in my aspx file.
If I create a new Session object in the else part, than I may loose my information stored in the session like logged in user information
if (session != null)
{
HttpContext.Current.Sessio
}
else
{
}
string applPath = Request.ApplicationPath;
Server.Transfer(applPath + "/errorTemplate.aspx", true);
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah. It helps
Many Thanks
Many Thanks
You have to check if the value of the HttpContext.Current.Sessio
Open in new window