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

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.
Keith
rowekAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
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"))
                {
                    Context.Response.Redirect("~/default.aspx");
                }
            }
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rowekAuthor Commented:
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.
0
rowekAuthor Commented:
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
                    Context.Response.Redirect("~/default.aspx")
                End If
            End If
        End If
       
       
    End Sub
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

rowekAuthor Commented:
Kyle, you are gonna LOVE this discovery.  My C# to VB.net converter changed
Application_PreRequestHandlerExecute
to
Application_PreRequestHandlerExecutive

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.  

Keith
0
rowekAuthor Commented:
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.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
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:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.