Solved

Exception handling C#

Posted on 2007-11-23
6
1,796 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
ID: 20340540
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
ID: 20349733
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
ID: 20350164
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:tia_kamakshi
ID: 20350503
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
ID: 20351276
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
ID: 20356623
Yeah. It helps

Many Thanks
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

778 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