Getting at the Session - Safely? HOW

Last post 05-13-2008 5:34 PM by nisarkhan. 12 replies.

Sort Posts:

  • Getting at the Session - Safely? HOW

    05-13-2008, 10:09 AM
    • Loading...
    • nisarkhan
    • Joined on 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,123

    I need to set a variable to a session variable (if that's what you call it)
    like this:

    dim ds as dataset = HttpContext.Current.Session("CustDataSet")

    But I get an exception if this variable in the current session hasn't been
    set yet so I need to test it. I tried:

    If HttpContext.Current.Session("CustDataSet") = nothing then
    HttpContext.Current.Session("CustDataSet") = GetCustDataSet()
    End If

    But I get an exception on this also and the exception message is:

    "Object reference not set to an instance of an object."

    How can I test for this?

    Its all about coding!
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 10:18 AM
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 1,005
    That looks like it should work. Even this line dim ds as dataset = HttpContext.Current.Session("CustDataSet") works ok for me. Which line exactly is it erroring on? Maybe post some more of your code up so we can see the exact order you're doing things.
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 10:20 AM
    • Loading...
    • anas
    • Joined on 09-21-2006, 8:31 AM
    • Jerusalem
    • Posts 4,320

    you are comparing reference types, so use is instead of =

     

            If HttpContext.Current.Session("CustDataSet") Is Nothing Then
                HttpContext.Current.Session("CustDataSet") = GetCustDataSet()
            End If
     

     

    if you still have problems  . we need to see  "GetCustDataSet" implementation , and where did you palced that code ?

    can you add a break point and make sure that the HttpContext is not null(nothing ) ?

     

     

    Regards,

    Anas Ghanem| Blog
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 10:27 AM
    • Loading...
    • nisarkhan
    • Joined on 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,123

    the above code was a sample lines to get an idea

    here is my real code which is royal pain to me:

    public class PageRoles : MyBasePage  //mybasepage is inheriting from  System.Web.UI.Page
     {
            public PageRoles() { }
            public static ArrayList LandingPage()
            {
                if (HttpContext.Current.Session["LPage"] == null//<<<<throwing error
                {
                        ArrayList roleNames = new ArrayList();
                        roleNames.Add(RoleNames.GUARD);
                        roleNames.Add(RoleNames.GUARD1);


                       HttpContext.Current.Session["LPage"] = (ArrayList)roleNames;
                        return roleNames;
                    }
                 
                return (ArrayList)HttpContext.Current.Session["LPage"];
            }
            }
        }

     
    Its all about coding!
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 10:28 AM
    • Loading...
    • nisarkhan
    • Joined on 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,123
    error is: "Object reference not set to an instance of an object."
    Its all about coding!
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 10:35 AM
    Answer
    • Loading...
    • anas
    • Joined on 09-21-2006, 8:31 AM
    • Jerusalem
    • Posts 4,320

    you are trying to access the httpContext before its being initialized,

    the solution is to exit the function if the HttpContext.currnet is null

     

           public static ArrayList LandingPage()
            {
               if( HttpContext.Current==null) return new Arraylist();
                if (HttpContext.Current.Session["LPage"] == null)  //<<<<throwing error
                {
                        ArrayList roleNames = new ArrayList();
                        roleNames.Add(RoleNames.GUARD);
                        roleNames.Add(RoleNames.GUARD1);
    
    
                       HttpContext.Current.Session["LPage"] = (ArrayList)roleNames;
                        return roleNames;
                    }
                  
                return (ArrayList)HttpContext.Current.Session["LPage"]; 
            }
            }
    
     
    Regards,

    Anas Ghanem| Blog
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 10:39 AM
    • Loading...
    • nisarkhan
    • Joined on 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,123

    i'm getting the error in:

    Line 23:         {
    Line 24:             if (HttpContext.Current == null) return new ArrayList();
    Line 25:             if (HttpContext.Current.Session["LPage"] == null)  //<<<<throwing error
    Line 26:             {
    Line 27:                 ArrayList roleNames = new ArrayList();

    Object reference not set to an instance of an object.

     

    Its all about coding!
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 10:42 AM
    • Loading...
    • anas
    • Joined on 09-21-2006, 8:31 AM
    • Jerusalem
    • Posts 4,320

    Yes, we must check the sesion also ,

    if (HttpContext.Current == null || HttpContext.Current.Session==null) return new ArrayList();
      if (HttpContext.Current.Session["LPage"] == null)
    .....

     

    Regards,

    Anas Ghanem| Blog
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 10:57 AM
    • Loading...
    • nisarkhan
    • Joined on 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,123

    Anas:

    your approach exists the moethd if the HttpContext.Current == null and i'm not looking for that

    what i'm looking is, if the HttpContext.Current.Session["LPage"] == null then create a ArrayList and add that ArrayList to a Session variable called "LPage"

     thanks for looking.

    shukran

    Its all about coding!
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 11:01 AM
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 1,005

    The problem is that the line of code you say is erroring shouldn't be causing a problem. I can execute this code without an error:

    if (HttpContext.Current.Session["LPage"] == null)
    {
        HttpContext.Current.Session["LPage"] = "SOME VARIABLE";
    }
    
    This is why anas was trying to check to see if HttpContext.Current or HttpContext.Current.Session was null in case the error originated from them and not from the Session variable itself. Is there more to your code that you haven't posted?
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 11:08 AM
    • Loading...
    • nisarkhan
    • Joined on 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,123

    i have no idea why this is not setting... anyway

    here is my full code

    public partial class Default2 : MyBasePage
    {
        ArrayList roleNames = PageRoles.LandingPage();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsUserAuthenticatedForPage(roleNames))
            {
                Response.Write("welcome!");
            }
            else
            {
                Response.Write("you do not have access!");
            }

        }
    }

      public class PageRoles : MyBasePage
        {
            public PageRoles() { }


            public static ArrayList LandingPage()
            {
                //if (HttpContext.Current == null || HttpContext.Current.Session == null) return new ArrayList();
                //if (HttpContext.Current.Session["LPage"] == null)
                //if (HttpContext.Current == null) return new ArrayList();
                if (HttpContext.Current.Session["LPage"] == null)  //<<<<throwing error
                {
                    ArrayList roleNames = new ArrayList();
                    roleNames.Add(RoleNames.GUARD);
                    roleNames.Add(RoleNames.GUARD1);
                    HttpContext.Current.Session["LPage"] = (ArrayList)roleNames;
                    return roleNames;
                }
                return (ArrayList)HttpContext.Current.Session["LPage"];
            }
    }

     public class MyBasePage : System.Web.UI.Page
        {
            public MyBasePage()
            {
                //this.Load += new EventHandler(BasePage_Load);
            }

            public enum RoleNames
            {
                GUARD,
                GUARD1,
                GUARD2
            }
    }

     WEB.CONFIG

     <system.web>
      <pages enableSessionState="true" enableEventValidation="false" >

    Its all about coding!
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 11:17 AM
    Answer
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 1,005

    I think anas was right (he usually is). You're calling LandingPage() before the Session has been set up. If you debug and look at HttpContext.Current.Session you'll see it's null, which is why you're getting an error. What you can do is move the call to LandingPage() into your Page_Load by which time the Session should be available, e.g.

    public partial class Default2 : MyBasePage
    {
    ArrayList roleNames;
    protected void Page_Load(object sender, EventArgs e)
    {
    roleNames = PageRoles.LandingPage();
    if (IsUserAuthenticatedForPage(roleNames))
    {
    Response.Write("welcome!");
    }
    else { Response.Write("you do not have access!");
    }
    }
    }
      
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: Getting at the Session - Safely? HOW

    05-13-2008, 5:34 PM
    • Loading...
    • nisarkhan
    • Joined on 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,123

    oops... i never paid attention to when i'm setting the session Angry

    thank you so much Anas and Ramblor for helping me to understand. Yes

     

    Its all about coding!
Page 1 of 1 (13 items)
Microsoft Communities