ASP.NET session becomes empty after postback on local

Here is my code that runs when the page loads:

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();
    }
}

Sometimes, when I try to save some data in the database and get an error message, I try to save the data again by clicking the save button, and I get this error:

Object reference not set to object instance

in the next line if the code:

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

Why am I getting this error?

here is my code in the user control, where ChR is a checkbox to remember the user:

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");
            }

and on my main page I assign values ​​to sessions this way in the page_Load event:

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
+3
source share
4 answers

, , ! db , , , !!!

+5

, .

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;
    }
}
+1

Why am I getting this error?

Because it Session["AccessLevel"]is Null, and you are trying to call a method on a Null object.

0
source

For other users, set the session timeout. maybe this is a problem. you can change the default timeout using

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

   <sessionState timeout="60"  />
</system.web>
0
source

All Articles