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
source
share