Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-09-14
8
Medium Priority
?
351 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

722 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