Solved

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

Posted on 2010-09-14
8
310 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
  • 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now