Solved

Session expired but doesnt redirect, throws an error

Posted on 2013-01-06
9
227 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 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