Solved

Session expired but doesnt redirect, throws an error

Posted on 2013-01-06
9
223 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net mvc return json 2 57
Code works but I need to redo it and assign values to Model 7 33
ASP.net Javascript use multiple sliders 2 19
Reset asp.net login password 4 34
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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.…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

22 Experts available now in Live!

Get 1:1 Help Now