Solved

Session expired but doesnt redirect, throws an error

Posted on 2013-01-06
9
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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