[英]ASP / C# Session Variable - Object reference not set to an instance of an object
I'm somewhat new to ASP / C# and I'm having an issue (probably simple) with sessions variables. 我对ASP / C#有点新,我遇到了会话变量的问题(可能很简单)。 My project has a Site.Master in which session variables are set under the Page_Load method like so: 我的项目有一个Site.Master,其中会话变量在Page_Load方法下设置,如下所示:
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;
}
}
This does work. 这确实有效。 If I dump out the session variable 'UserGroups' to a label or something within this method, it does display the variable contents correctly. 如果我将会话变量'UserGroups'转储到此方法中的标签或其他内容,它会正确显示变量内容。
So, my problem lies within another page (say default.aspx) when I try to access that same session variable. 所以,当我尝试访问同一个会话变量时,我的问题在于另一个页面(比如default.aspx)。 In the Page_Load method of the other page I attempt to do this: 在另一页的Page_Load方法中,我尝试这样做:
protected void Page_Load(object sender, EventArgs e)
{
string GroupList = HttpContext.Current.Session["UserGroups"].ToString();
//some code with the variables here
}
This always fails with an "Object reference not set to an instance of an object." 这始终失败,并且“对象引用未设置为对象的实例”。 error. 错误。 Am I trying to get the Session variable wrong? 我试图让Session变量错误吗? I've tried 我试过了
string GroupList = Session["UserGroups"].ToString();
this also errors with the same error. 这也是同样错误的错误。
string GroupList = (string)(Session["UserGroups"]);
This always returns an empty string. 这总是返回一个空字符串。
What am I doing wrong? 我究竟做错了什么?
Thanks! 谢谢!
The syntax you are using in your Page_Load
method I wouldn't have even expected to compile. 您在Page_Load
方法中使用的语法我甚至不希望编译。 Regardless, the issue is that you have not set the Session with that key, so it will return null
. 无论如何,问题是您没有使用该键设置Session,因此它将返回null
。 When you call ToString()
on that null, you get the exception. 当您在该null上调用ToString()
时,您将获得异常。 In your second example: 在你的第二个例子中:
string groupList = (string)(Session["UserGroups"])
That is doing a conversion of null to string
, which results in a null string (thus not causing the exception). 那就是将null转换为string
,这会产生一个空字符串(因此不会导致异常)。
You should be able to rewrite your Page_Load
implementation like so: 你应该能够像这样重写你的Page_Load
实现:
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 so: 然后,在稍后访问会话变量时,请执行以下操作:
string userGroup = Session["UserGroups"] as string;
This is a safe way to attempt the conversion of whatever bucket that is in session into a string. 这是尝试将会话中的任何存储桶转换为字符串的安全方法。 If the key doesnt exist, or the value is not a string, you will get null
. 如果密钥不存在,或者该值不是字符串,则将为null
。 Otherwise, you will get the string from that hash. 否则,您将从该哈希中获取字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.