Solved

Object reference not set to an instance of an object session lost

Posted on 2010-09-14
8
348 Views
Last Modified: 2012-05-10
Hi experts, I need to validate if a user loses the session, meaning that it redirects to the login page. I attaced the error
sinSESSION.jpg
0
Comment
Question by:enrique_aeo
[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
  • 5
  • 3
8 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 33677880
you can put a null check on session before using it like:

if(Session["userConnected"]==null){
     Resposne.Redirect("~/login.aspx");
}
else{
//use it
}
0
 

Author Comment

by:enrique_aeo
ID: 33681819
ok, I'm using that session variable 35 times, is there another way to do it?
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33682850
You mean same page or different pages?
Is that going to be in a page code-behind that uses master page?
If yes, then you can do that check in your MasterPags's Page_Load..i mean then you will need an if statement.

Another option is using a BasePage: http://weblogs.asp.net/scottgu/archive/2005/08/02/421405.aspx
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:enrique_aeo
ID: 33682910
Interestingly, you could help me with the code of a page based on this case of the session
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33682994
sorry, what's it that you want me to help you with?
0
 

Author Comment

by:enrique_aeo
ID: 33683016
I use a base page, if possible I want to help me with the code that should have that page to check if the session is null
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 33683410
sample here:
BasePage.cs in App_Code Folder:

using System;
using System.Web;
public class BasePage: System.Web.UI.Page
{
    protected override void OnLoad(EventArgs e)
    {

        if (Session["userConnected"] == null)
        {
           Response.Redirect("~/login.aspx");
        }  

        base.OnLoad(e);

    }  
}

Code-behind of sample Page using BasePage:
SamplePage.aspx.cs

public partial class BasePageTest : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //This code will be reached only if Session["userConnected"] is not null
        string s = Session["userConnected"].ToString();
    }
}
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33683433
typo...  BasePageTest.aspx.cs not SamplePage.aspx.cs
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

632 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