ASP.net  "Object reference not set to an instance of an object"

Posted on 2014-10-21
Medium Priority
Last Modified: 2014-10-21
I know this is an age old problem, but I am having trouble with some legacy WebForms ASP.Net stuff that I must support.  The code is well written, but the developer never took into account the predictable loss of session state after the default 20 minute timeout.  

I know I can bump the timeout up a bit, but that is not the best solution.  I just want the code to sense an error (globally) and reroute itself to the home page and throw a simple error such as, "Your session timed out".

I have other web sites that do just this. But what do I need to implement to make this happen?  I don't want to go to each page of legacy code to check for a session variable.  

Thanks in advance.
Question by:rowek
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 40

Accepted Solution

Kyle Abrahams earned 2000 total points
ID: 40395535
You can redirect to a page using the global.asax:
/// <summary>
    /// The event occurs just after Initialization of Session, and before Page_Init event
    /// </summary>
    protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
        // here it checks if session is required, as
        // .aspx requires session, and session should be available there
        // .jpg, or .css does require session so session will be null
        // as .jpg, or .css are also http request in either case if you implemented URL Rewritter, or custom IHttp Module
        if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
            // here is your actual code
            // check if session is new one
            // or any of your logic
            if (Session.IsNewSession                || Session.Count < 1)
                // checking if request is not for default.aspx page, as it should not be redirected
                if (!Context.Request.Url.AbsoluteUri.ToLower().Contains("/default.aspx"))

Open in new window


Author Comment

ID: 40395732
Kyle, just want to make sure I understand the code.  There must be an event called Application_PreRequestHandlerExecute.  Your documentation says "The event occurs just after Initialization of Session".  Is this a sure fire way to capture the timeout?  I will try to implement.  

It should not matter that the legacy app (ASP.NET 2.0) is written in VB.NET, does it?  It has been upgraded to .NET 4.0.  We had to stick with 4.0 because it runs on a 2003 Server.

Author Comment

ID: 40395754
Kyle, the event never fires.  The solution seems very elegant, but I don't get my event.  Here is my converted code:

    Sub Application_PreRequestHandlerExecutive(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs just after Initialization of Session and before Page_Init event
        If TypeOf Context.Handler Is IRequiresSessionState OrElse TypeOf Context.Handler Is IReadOnlySessionState Then
            ' check if session is new one
            ' or any of your logic
            If Session.IsNewSession OrElse Session.Count < 1 Then
                ' checking if request is not for default.aspx page, as it should not be redirected
                If Not Context.Request.Url.AbsoluteUri.ToLower().Contains("/default.aspx") Then
                End If
            End If
        End If
    End Sub
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 40395758
Kyle, you are gonna LOVE this discovery.  My C# to VB.net converter changed

I cannot make this stuff up!  Once I renamed my event procedure back to the name you provided it worked great!  Thank you very much.  


Author Closing Comment

ID: 40395783
Kyle's code was perfect.  My language converter played games with me, but once I found the issue the code worked great.  Believe it or not, I have been coding ASP.Net web sites since 1.0 and never found a solution to this common problem as elegant as this one.  Great job, Kyle.
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40396181
Sorry for not responding earlier.  Glad you were able to work through it.  In the future you should tag VB as asp.net can be both VB and C#.  I program mostly in C# these days which is why you got that solution.

A good online converter:

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

801 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