Solved

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

Posted on 2014-10-21
6
703 Views
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.
Keith
0
Comment
Question by:rowek
  • 4
  • 2
6 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 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"))
                {
                    Context.Response.Redirect("~/default.aspx");
                }
            }
        }
    }

Open in new window

0
 

Author Comment

by:rowek
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.
0
 

Author Comment

by:rowek
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
                    Context.Response.Redirect("~/default.aspx")
                End If
            End If
        End If
       
       
    End Sub
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:rowek
ID: 40395758
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
 

Author Closing Comment

by:rowek
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.
0
 
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:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

14 Experts available now in Live!

Get 1:1 Help Now