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 PatelPosted Dec 2, 2022, 12:50 PM
Hi Ahmed Khalid,
For your given code you can try as following:
For more details:
Hope this will help you!
Ahmed KhalidPosted Dec 2, 2022, 5:03 PM
Thanks Patel. The code has worked. No more error messages
Ahmed KhalidPosted Dec 2, 2022, 6:36 AM
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("");
}
}
Pankajkumar PatelPosted Dec 2, 2022, 4:46 AM
Hi Ahmed Khalid,
First check not equal to null and than equals as like following:
Hope this will help you!