Solved

Session expired but doesnt redirect, throws an error

Posted on 2013-01-06
9
224 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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