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?