Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Session becomes null after postback on local

Tags:

c#

asp.net

Here is my code that runs on page load:

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 to database and I get an error, I try to re-save the data by clicking my save button, and I get this error:

Object reference not set to an instance of an object

on the following line if code:

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

Why am I getting this error?

here is my code in Login user control where ChR is the 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 in my master 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
like image 850
gwt Avatar asked Aug 16 '26 06:08

gwt


2 Answers

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 !!!

like image 116
gwt Avatar answered Aug 18 '26 19:08

gwt


Replace your code like below.

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

}

I hope you understand the difference between == and Equals()

public static String AccessLevel {
    get
    {
        return Convert.ToString(HttpContext.Current.Session["AccessLevel"]);
    }
    set
    {
        HttpContext.Current.Session["AccessLevel"] = value;
    }
}
like image 27
Pankaj Avatar answered Aug 18 '26 18:08

Pankaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!