I'm a little new to ASP / C # and I have a problem (possibly simple) with session variables. My project has Site.Master, in which session variables are set under the Page_Load method as follows:
protected void Page_Load(object sender, EventArgs e) { if ((Session)["UserID"]==null || (Session)["UserID"].ToString() == "") { (Session)["UserID"] = HttpContext.Current.User.Identity.Name.ToString(); SqlDataReader dr = Sprocs.GetPermissionGroups(); string groupList = ""; while (dr.Read()) { if (groupList != "") { groupList = groupList + "|" + dr["WG_Group"].ToString(); } else { groupList = dr["WG_Group"].ToString(); } } dr.Close(); if (groupList != "") { (Session)["UserGroups"] = groupList; } }
It works. If I reset the "UserGroups" session variable to a label or something in this method, it correctly displays the contents of the variable.
So my problem is in another page (say default.aspx) when I try to access the same session variable. In the Page_Load method of another page, I am trying to do this:
protected void Page_Load(object sender, EventArgs e) { string GroupList = HttpContext.Current.Session["UserGroups"].ToString();
This always happens with the error "Link to an object that is not installed on an instance of the object." error. Am I trying to get the Session variable incorrectly? I tried
string GroupList = Session["UserGroups"].ToString();
these are also errors with the same error.
string GroupList = (string)(Session["UserGroups"]);
This always returns an empty string.
What am I doing wrong?
Thanks!
Seril source share