Hi,
I have implemented Session in Master Page and in Login Pages for User and Admin in my application. The application is working fine but it frequently prompts "Object reference not set to an instance of an object." during Login process and whenever the home page is being displayed or page gets refreshed and it stops at if (Session["role"].Equals("")) in debug mode
I tried to comment the if (Session["role"].Equals("")) and keep the if (Session["role"].Equals("user")) and if (Session["role"].Equals("admin")) un commented. But behaviour remains the same.
Any idear please?

Pankajkumar PatelRank #181 · 13.4K reputationPosted Dec 2, 2022, 7:50 AM EST
Hi Ahmed Khalid,
For your given code you can try as following:
protected void Page_Load(object sender, EventArgs e) { string Status = "Hi"; if (Session["role"] == null) { // set session Status = "null"; } else { // do some thing here if (Session["role"].Equals("user")) { Status = "user"; } else if (Session["role"].Equals("Admin")) { Status = "Admin"; } } Response.Write(Status); }For more details:
Hope this will help you!
Pankajkumar PatelRank #181 · 13.4K reputationPosted Dec 1, 2022, 11:46 PM EST
Hi Ahmed Khalid,
First check not equal to null and than equals as like following:
if (Session["role"] != null && Session["role"].Equals("user")Hope this will help you!
Ahmed Khalid185 reputationPosted Dec 2, 2022, 1:36 AM EST
Hi Patel,
i have checked but it returns the same error. Kindly find the full code below:
protected void Page_Load(object sender, EventArgs e)
{
try
{
//if (Session["role"] != null && Session["role"].Equals("user")) //suggested code
if (Session["role"].Equals("")) //old code
{
Btn_Login.Visible = true; // user login link button
Btn_MyProfile.Visible = false; // sign up link button
Btn_Logout.Visible = false; // logout link button
Btn_HelloUser.Visible = false; // hello user link button
Btn_AdminLogin.Visible = true; // admin login link button
Btn_ManageParents.Visible = false; // author management link button
Btn_ManageUsers.Visible = false; // publisher management link button
}
if (Session["role"].Equals("user"))
{
Btn_Login.Visible = false; // user login link button
Btn_MyProfile.Visible = false; // sign up link button
Btn_Logout.Visible = true; // logout link button
Btn_HelloUser.Visible = true; // hello user link button
Btn_HelloUser.Text = "Hello " + Session["FullName"].ToString(); // display hello user string
Btn_AdminLogin.Visible = true; // admin login link button
Btn_ManageParents.Visible = false; // author management link button
Btn_ManageUsers.Visible = false; // publisher management link button
}
else if (Session["role"].Equals("Admin"))
{
Btn_Login.Visible = false; // user login link button
Btn_MyProfile.Visible = false; // sign up link button
Btn_Logout.Visible = true; // logout link button
Btn_HelloUser.Visible = true; // hello user link button
Btn_HelloUser.Text = "Hello Admin"; // display hello Admin string
Btn_AdminLogin.Visible = false; // admin login link button
Btn_ManageParents.Visible = true; // author management link button
Btn_ManageUsers.Visible = true; // publisher management link button
}
}
catch (Exception ex)
{
Response.Write("");
}
}
And in Login Pages for Admin and User, i have implemented below code on Login Button click:
protected void btn_Login_Click1(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(strcon);
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("Select * from [Admin_Tbl] " +
"where [UserName] = '" + txt_userName.Text.Trim() + "' and " +
"[Password] = '" + txt_Password.Text.Trim() + "' and [Status] = 'Active'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
//Response.Write("");
// Session["role"] = "user"; // for User Login Page
Session["role"] = "Admin"; // for Admin Login Page
Session["FullName"] = dr.GetValue(1).ToString();
Session["LoginName"] = dr.GetValue(2).ToString();
Session["UserType"] = dr.GetValue(5).ToString();
//Session["UserStatus"] = dr.GetValue(11).ToString();
}
Response.Redirect("homepage.aspx");
}
else
{
Response.Write("");
}
}
catch (Exception ex)
{
Response.Write("");
}
}
Ahmed Khalid185 reputationPosted Dec 2, 2022, 12:03 PM EST
Thanks Patel. The code has worked. No more error messages