Shortcuts

Null Value in Session Variable

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 mydefault.aspxI have the following code that runs on thepage_init

protectedvoidPage_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 setUserObject 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

I setEnableSessionState = Trueon 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.

Re: Null Value in Session Variable

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: