3

这是我在页面加载时运行的代码:

protected void Page_Load(object sender, EventArgs e)
{
    DisableChaching();
    if (Request.Cookies["UserName"] == null)
    {
        if (Session["UserName"] == null)
        {
            Response.Redirect("~/Default.aspx");
        }
        else if (Session["AccessLevel"].ToString().Equals("2"))
        {
            Response.Redirect("~/Default.aspx");
        }
    }
    else if (Session["AccessLevel"].ToString().Equals("2"))
    {
        Response.Redirect("~/Default.aspx");
    }
    if (!IsPostBack)
    {
        LoadControls();
        BindGrid();
    }
}

有时,当我尝试将一些数据保存到数据库并出现错误时,我尝试通过单击保存按钮重新保存数据,但出现此错误:

你调用的对象是空的

如果代码如下:

else if (Session["AccessLevel"].ToString().Equals("2"))

为什么我会收到此错误?

这是我在登录用户控件中的代码,其中 ChR 是记住用户的复选框:

if (ChR.Checked == true)
            {
                Response.Cookies["UserName"].Value = txtUserName.Text.Trim();
                Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(2);
                Response.Cookies["AccessLevel"].Value = member.AccessLevel.ToString();
                Response.Cookies["AccessLevel"].Expires = DateTime.Now.AddMonths(2);
                Response.Cookies["FirstName"].Value = member.FirstName;
                Response.Cookies["FirstName"].Expires = DateTime.Now.AddMonths(2);
                Response.Cookies["LastName"].Value = member.LastName;
                Response.Cookies["LastName"].Expires = DateTime.Now.AddMonths(2);
                Session["UserName"] = txtUserName.Text.Trim();
                Session["AccessLevel"] = member.AccessLevel.ToString();
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                Session["UserName"] = txtUserName.Text.Trim();
                Session["AccessLevel"] = member.AccessLevel.ToString();
                Session["FirstName"] = member.FirstName;
                Session["LastName"] = member.LastName;
                Response.Redirect("~/Default.aspx");
            }

在我的母版页中,我在 page_Load 事件中以这种方式为会话分配值:

DisableChaching();
    FillInfo();
    if (Request.Cookies["UserName"] != null)
    {
        Session["UserName"] = Request.Cookies["UserName"].Value;
        Session["AccessLevel"] = Request.Cookies["AccessLevel"].Value;
        Session["FirstName"] = Request.Cookies["FirstName"].Value;
        Session["LastName"] = Request.Cookies["LastName"].Value;
        WellCome();
        if (Session["AccessLevel"].ToString() == "1")
        {
            RenderMenu(AcccessLevel.SiteManager);
        }
        else if (Session["AccessLevel"].ToString() == "2")
        {
            RenderMenu(AcccessLevel.Client);
        }
    }
    else if (Session["UserName"] != null)
    {
        WellCome();
        if (Session["AccessLevel"].ToString() == "1")
        {
            RenderMenu(AcccessLevel.SiteManager);
        }
        else if (Session["AccessLevel"].ToString() == "2")
        {
            RenderMenu(AcccessLevel.Client);
        }

    }
    else
    {
        WellGo();
        RenderMenu(AcccessLevel.LogedOutUser);
    }
enter code here
4

4 回答 4

6

I think I found out why my sessions become null ! when I want to save info about a book in db and save an image file of the book in an app folder , I don't have the permission to save the file and my application throws an error and all my sessions become null !!!

于 2012-05-04T07:56:46.923 回答
1

替换您的代码,如下所示。

string val = Convert.ToString(Session["AccessLevel"]);
if (val == "2")
{

}

==我希望你明白和之间的区别Equals()

public static String AccessLevel {
    get
    {
        return Convert.ToString(HttpContext.Current.Session["AccessLevel"]);
    }
    set
    {
        HttpContext.Current.Session["AccessLevel"] = value;
    }
}
于 2012-05-03T15:51:17.303 回答
0

为什么我会收到此错误?

因为Session["AccessLevel"]是 Null 并且您正在尝试调用 Null 对象上的方法。

于 2012-05-03T15:46:43.920 回答
0

For other users, check your session timeout. may be it is the problem. you can change the default timeout using

<system.web>
   <authentication mode="Forms">
      <forms timeout="50"/>
   </authentication>

   <sessionState timeout="60"  />
</system.web>
于 2015-08-14T14:04:04.527 回答