Solved

Exception handling C#

Posted on 2007-11-23
6
1,806 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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.

830 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