Solved

Exception handling C#

Posted on 2007-11-23
6
1,774 Views
Last Modified: 2008-02-01
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.Session.Add("LastError", handledException);

but at line
HttpContext.Current.Session.Add("LastError", handledException);

it says

An exception of type 'System.NullReferenceException' occurred in App_global.asax.lp6-olqc.dll 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/HomeP098098age.aspx' does not exist.
   at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
   at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
   at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
   at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
   at System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
   at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
   at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)}
   


void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
        HttpContext.Current.Response.ClearContent();
        Exception handledException = Server.GetLastError();
        HttpContext.Current.Session.Add("LastError", 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

0
Comment
Question by:tia_kamakshi
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
The code in the Application_Error method assumes that there is a Session object in the current context, but when the error is that the requested page doesn't exist, the Session object hasn't been created yet.

You have to check if the value of the HttpContext.Current.Session property is null or not, before you try to use it:
Exception handledException = Server.GetLastError();

HttpSessionState session = HttpContext.Current.Session;

if (session != null) {

   session.Add("LastError", handledException);

} else {

   // handle the error message differently.

}

Open in new window

0
 

Author Comment

by:tia_kamakshi
Comment Utility
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
0
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:tia_kamakshi
Comment Utility
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.Session.Add("LastError", handledException);
}
else
{
}
 string applPath = Request.ApplicationPath;
 Server.Transfer(applPath + "/errorTemplate.aspx", true);
 
 
 Thanks
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
Comment Utility
I'm not sure, but the context should survive the transfer, so that you could store the information in the context object:

HttpContext.Current.Items["LastError"] = handledException;
0
 

Author Comment

by:tia_kamakshi
Comment Utility
Yeah. It helps

Many Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now