Solved

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

Posted on 2010-09-14
8
331 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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