Solved

Session expired but doesnt redirect, throws an error

Posted on 2013-01-06
9
222 Views
Last Modified: 2013-01-11
I have the code below to redirect when the session is expired. Instead of redirecting after the session is expired, it throws this error

Object reference not set to an instance of an object.

Why is that? why isn't it redirecting? how can I fix it?

private int BusinessNameId
    {
        get
        {
            if (string.IsNullOrEmpty(Session["BusinessId"].ToString()))
            {
                Response.Redirect("~/SessionExpired.aspx");
            }
            return int.Parse(Session["BusinessId"].ToString());
        }
    }

Open in new window

0
Comment
Question by:Camillia
  • 4
  • 3
  • 2
9 Comments
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 250 total points
ID: 38748868
You need to check for null
if(Session["BusinessId"] == null)
{
    Response.Redirect("~/SessionExpired.aspx");
return null;
}
else
return int.Parse(Session["BusinessId"].ToString());

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 38748902
doesnt IsNullOrEmpty check for null?
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38749660
Session["BusinessId"].ToString() should throw an object reference error.
0
 
LVL 7

Author Comment

by:Camillia
ID: 38749677
But i have it in a IsNullOrEmpty..

Now, If I change it to below..in that case it goes to redirect line. No object null error. Why is it like that...is it because string businessId = Session["BusinessId"].ToString();
initializes it to an empty string?


private int BusinessNameId
    {
        get
        {
             string businessId = Session["BusinessId"].ToString();
            if (string.IsNullOrEmpty(businessId))
            {
                Response.Redirect("~/SessionExpired.aspx");
            }
            return int.Parse(Session["BusinessId"].ToString());
        }
    }
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 27

Accepted Solution

by:
Chinmay Patel earned 250 total points
ID: 38749693
Hi Farzadw,

No. Problem is this statement:
Session["BusinessId"].ToString();

What happens if your session is expired, Session["BusinessId"] will return a null.

Now your code is always executed from most inner to most outer brackets, so it will first try to execute your .ToString() method on a null object and that will throw an exception even before your string.IsNullOrEmpty get's a chance to execute itself.
Also another trick you can use for such scenarios is to use as operator

  string businessId = Session["BusinessId"] as string;

Regards,
Chinmay.
0
 
LVL 7

Author Comment

by:Camillia
ID: 38749715
So I should change it what informaniac has?
0
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 38749723
Even this should work:
private int BusinessNameId
    {
        get
        {
             string businessId = Session["BusinessId"] as string;
            if (string.IsNullOrEmpty(businessId))
            {
                Response.Redirect("~/SessionExpired.aspx");
            }
            return int.Parse(Session["BusinessId"].ToString());
        }
    } 

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 38749729
thanks, it makes sense now
0
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 38749738
glad I could help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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…

762 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

20 Experts available now in Live!

Get 1:1 Help Now