ASP / C # Session Variable - object reference is not installed in the object instance

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(); //some code with the variables here } 

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!

+4
source share
1 answer

The syntax that you use in your Page_Load method, I would not even expect compilation. Despite this, the problem is that you did not establish a session with this key, so it will return null . When you call ToString() at this zero, you get an exception. In the following example:

 string groupList = (string)(Session["UserGroups"]) 

This does the conversion from null to string , which results in an empty string (thereby causing no exception).

You should be able to rewrite the Page_Load implementation as follows:

 string sessionUserId = Session["UserId"] as string; if(string.IsNullOrEmpty(sessionUserId)) { 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(); Session["UserGroups"] = groupList; } 

Then, when accessing the session variable later, do it like this:

  string userGroup = Session["UserGroups"] as string; 

This is a safe way to try to convert any bucket in a session into a string. If the key does not exist or the value is not a string, you will get null . Otherwise, you will get a string from this hash.

+7
source

Source: https://habr.com/ru/post/1388484/


All Articles