Null Value in Session Variable RRS feed

  • Question

  • User682008122 posted

    I'm having an issue retrieving a session variable on a page different than where it was set. The session variable holds a User Class Object that contains about 15 different user attributes for the current user.

    I need this available on all of the pages and don't want to requery the database every time I land on a page. On my default.aspx I have the following code that runs on the page_init

    protected void Page_Init(object sender, EventArgs e)
        // Get the logged in user id with acct domain (ie. acct05\abc123)
        masterNtId = Request.ServerVariables["LOGON_USER"].ToString();
        // Get the logged in user id (ie. abc123)
        masterNtID_short = masterNtId.Substring(masterNtId.IndexOf("\\") + 1);
        // Get the current page (ie. /PMM/page_name.aspx)
        UserObject user = TheUser.User.SearchUserWhitePages(masterNtID_short);
        Session["UserProfile"] = user;
        //Added to test that session is being set
        UserObject theuser = (UserObject)Session["UserProfile"];
        string name = theuser.get_UserName();

    It successfully returns the session variable. However when I add the following code on another page that I navigate too from the default.aspx I get this error

    "An exception of type 'System.NullReferenceException' occurred"

    protected void Page_Load(object sender, EventArgs e)
        UserObject theuser = (UserObject)Session["UserProfile"];
        string name = theuser.get_UserName();

    I set EnableSessionState = True on the aspx pages. I'm not sure what I am forgetting to do. Should I be setting my session state variable differently?

    I've tried simplifying to just setting a single value into a session variable with the same results. I've tried creating methods for setting/retrieving the session variables and that didn't fix the issue. I am at 7 hrs and still can't get a session variable to persist between pages. I'm not sure what I'm missing.

    Monday, November 19, 2018 7:02 PM

All replies

  • User283571144 posted

    Hi Whalensdad,

    Could you please post the sessionstate setting you have used in your web.config?

    Like below:

    <sessionState mode="StateServer" cookieless="false" timeout="20"/>

    According to your error message, it seems you lose the Session value when you access new page.

    As far as I know, there are multiple reasons cause this issue.For exmaple, you disable the cookie in your browser.The server find the session according to sessionid in the requesst, if you don't send the sessionid to the server, it will face this exception. Normally, the browser will enable the cookie and auto send the sessionid in the requrest header to the sever.

    I suggest you could try to use browser F12 develop tool's network tag to check the request header.

    About how to use F12 to check the header, you could refer to below image:

    Best Regards,


    Tuesday, November 20, 2018 8:55 AM
  • User682008122 posted

    Hi Brando,

    I didn't explicitly set SessionState in the web.config. I used the EnableSessionState enumeration on each page. Looking at the following Microsoft Document, the InProc method is used: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/h6bb9cz9(v=vs.100).  I did add the following based on your question:  <sessionState mode="InProc" timeout ="20" cookieless="false"></sessionState>

    I looked at F12 and my view is different than what you show; I don't have a SessionState under the Name Column.  This is what I get on the page:

    Request URL: http://localhost:1605/FileUpload.aspx
    Request Method: GET
    Status Code: 200 / OK
    - Request Headers
    Accept: text/html, application/xhtml+xml, image/jxr, */*
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US
    Connection: Keep-Alive
    DNT: 1
    Host: localhost:1605
    Referer: http://localhost:1605/Default.aspx
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
    - Response Headers
    Cache-Control: private
    Content-Encoding: gzip
    Content-Length: 33890
    Content-Type: text/html; charset=utf-8
    Date: Tue, 20 Nov 2018 11:35:53 GMT
    Persistent-Auth: true
    Server: Microsoft-IIS/10.0
    Vary: Accept-Encoding
    X-AspNet-Version: 2.0.50727
    X-Frame-Options: SAMEORIGIN
    X-Powered-By: ASP.NET
    X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcbGFwb2xpa29cRG9jdW1lbnRzXFZpc3VhbCBTdHVkaW8gMjAxNVxQcm9qZWN0c1xKb2JMYXVuY2hOZXdccG1tbWVhc3VyZXMtZGV2LnNzYy5sbWNvLmNvbVxzaXRlXEZpbGVVcGxvYWQuYXNweA==?=

    Tuesday, November 20, 2018 11:40 AM
  • User283571144 posted

    Hi Whalensdad,

    According to your request details, I found your requrest header doesn't contain cookie.It seems your browser disable cookie. I suggest you could try to enable it.

    If you don't know how to enable the cookie for your browser, I suggest you could post what is your browser, IE or something else.

    Besides, if you don't want to enable the cookie, I suggest you could try to set cookieless attribute to true in the sessionstate tag in web.config.

    <sessionState mode="InProc" timeout ="20" cookieless="true"></sessionState>

    It will send the session id through the url instead of request cookie.

    Best Regards,


    Wednesday, November 21, 2018 5:26 AM