对象在分配给Session时失去参考?

时间:2010-10-14 13:56:06

标签: c# asp.net session-state

我面临一个非常奇怪的问题。我得到一个错误对象引用未设置为服务器上的对象实例,而代码在我的开发机器上运行正常。奇怪的部分是代码抛出错误的行号(如堆栈跟踪中所示)

DemoUser demoUser = new DemoUser();

if (!Request.QueryString.AllKeys.Contains("name"))
{
  playerName = usr.NinjaName;
  demoUser.Email = usr.UserEmail;
  demoUser.UserPicture = usr.UserPicture;
}
else
{
  playerName = Request.QueryString["name"];
  demoUser.Email = String.Empty;
  demoUser.UserPicture = "http://graph.facebook.com/1/picture";
}         

demoUser.EntryDateTime = DateTime.Now;
demoUser.Name = playerName;

Session["DemoUser"] = demoUser;

imgPlayer.ImageUrl = demoUser.UserPicture; // This is throwing error. 

如果该行抛出错误,那么当我将它分配给Session时,demoUser对象就会丢失。是吗?

2 个答案:

答案 0 :(得分:2)

您确定demoUser是空引用而不是imgPlayer吗?

答案 1 :(得分:0)

尝试:

demoUser = (DemoUser)Session["DemoUser"]; 

imgPlayer.ImageUrl = demoUser.UserPicture;