Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Developers,

I want to know on my website Home page, which user is login in his account. If anyone will be login, then his Username will be displayed on my website home page in his system. and if no one is login, then "Welcome Guest" will be displayed.

Please give me solution for this,
I already try it with
1. By use of session

if (Session["client_username"] == null)
{
lbl_user.Text = "Welcome Guest";
}
else
{
lbl_user.Text = username.ToString();

}

use of session, it is not working, it always show "Welcome Guest"

2 select form database table according to field of "IsLogin" status.

DataSet ds = c_client_login.status(Session["client_username"].ToString(), 1);
if (ds.Tables[0].Rows[0]["IsLogin"].ToString() != "1";)
{

lbl_user.Text = "Welcome Guest";

//txt_pric.Text = ds.Tables[0].Rows[0]["total_price"].ToString() + "/-";
}
else
{
lbl_user.Text = Session["client_username"].ToString();
//txt_pric.Text = "0/-";
}

2 way, it display and error of" Object reference not set to an instance



please give me a perfect solution
Posted
Comments
CoderPanda 22-Mar-14 13:51pm    
Where are you fetching the the user's name from the DB and assigning to Session["client_username"] ?
Neeraj_kaushik 22-Mar-14 14:00pm    
this code is placed on Master page, and also here i am fetching Username which Login stause is "1" according
Neeraj_kaushik 22-Mar-14 14:01pm    
c_client_login.status(Session["client_username"].ToString(), 1);

this line call the "status" function of " c_client_login table" and i fetch the username in class


public static DataSet status(string user_session, int IsLogin)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ToString();
// eshop_connection con = new eshop_connection();
SqlDataAdapter da = new SqlDataAdapter("select * from client_login where client_user_name=@client_user_name",con);
con.Open();
da.SelectCommand.Parameters.AddWithValue("@client_user_name", user_session);
da.SelectCommand.Parameters.AddWithValue("@IsLogin", IsLogin);

DataSet ds = new DataSet();
da.Fill(ds);
con.Close();

return ds;
}
Neeraj_kaushik 22-Mar-14 14:04pm    
My login page coding for get username

DataTable dt = c_client_login._GetBy_login(txt_unm.Text, txt_psd.Text);

if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[0]["client_user_name"].ToString() == txt_unm.Text.Trim() && dt.Rows[0]["password"].ToString() == txt_psd.Text.Trim())
{

Session["client_username"] = dt.Rows[0]["client_user_name"].ToString();
Session["client_code"] = dt.Rows[0]["client_code"].ToString();
//Session["client_ip"] = txt_ip.Text;
c_client_login._upagteBy_login(Session["client_username"].ToString(),1);

Response.Redirect("~/Client/Home.aspx");

}
else
{
Label3.Visible = true;
Label3.Text = "Login Info Is Not Correct";
}
CoderPanda 22-Mar-14 14:26pm    
Okay, can you please check if somehow nothing is being returned from DB in the following line?
Session["client_username"] = dt.Rows[0]["client_user_name"].ToString();
If it is getting something from DB, then is there any other place in the code where Session["client_username"] is being overwritten?

You just need to set the session variable. However you are logging them in set Session["client_username"] = someVariable; Then you can use it wherever you need it.
 
Share this answer
 
Comments
Neeraj_kaushik 22-Mar-14 14:21pm    
Please Read my last comment
or see here

My login page coding for get username

DataTable dt = c_client_login._GetBy_login(txt_unm.Text, txt_psd.Text);

if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[0]["client_user_name"].ToString() == txt_unm.Text.Trim() && dt.Rows[0]["password"].ToString() == txt_psd.Text.Trim())
{

Session["client_username"] = dt.Rows[0]["client_user_name"].ToString();
Session["client_code"] = dt.Rows[0]["client_code"].ToString();
//Session["client_ip"] = txt_ip.Text;
c_client_login._upagteBy_login(Session["client_username"].ToString(),1);

Response.Redirect("~/Client/Home.aspx");

}
else
{
Label3.Visible = true;
Label3.Text = "Login Info Is Not Correct";
}


and on Masterpage



if (Session["client_username"] == null)
{
lbl_user.Text = "Welcome Guest";
}
else
{
lbl_user.Text = username.ToString();

}

use of session, it is not working, it always show "Welcome Guest"

2 select form database table according to field of "IsLogin" status.

DataSet ds = c_client_login.status(Session["client_username"].ToString(), 1);
if (ds.Tables[0].Rows[0]["IsLogin"].ToString() != "1";)
{

lbl_user.Text = "Welcome Guest";

//txt_pric.Text = ds.Tables[0].Rows[0]["total_price"].ToString() + "/-";
}
else
{
lbl_user.Text = Session["client_username"].ToString();
//txt_pric.Text = "0/-";
}

2 way, it display and error of" Object reference not set to an instance
ZurdoDev 22-Mar-14 14:27pm    
This is a timing issue. All you need to do is put breakpoints and you'll see that your master page runs first, before you have filled it in. You need use a different event if you want it in the master page, perhaps onprerender.
Neeraj_kaushik 22-Mar-14 14:37pm    
i don't use onprerender before.
you can explain working of my query here,
try to understand your answer
ZurdoDev 22-Mar-14 15:43pm    
Refer to this link for the order of events of content pages and master pages.
http://msdn.microsoft.com/en-us/library/dct97kc3.ASPX
Just enter userID in to session during login like this.. :)


C#
Session["user"]= 5;


You can got it Any where in application till it will not expired like

C#
lblSession.Text=Session["user"].toString();


Set session timeout like this

C#
<configuration>
  <system.web>
     <sessionState timeout="20"></sessionState>
  </system.web>
</configuration>
 
Share this answer
 
v2
Comments
Neeraj_kaushik 22-Mar-14 14:41pm    
i already try on masterpage.
lblSession.Text=Session["user"].toString();

but it is not working and sometime it will display error of "Object reference not set to
an inistance"
Nirav Prabtani 22-Mar-14 14:48pm    
It gives error when session expires ...set session timing in web.config and convert it into string like that Convert.ToString() instead .ToString()
Neeraj_kaushik 22-Mar-14 14:52pm    
see my config file

<configuration>
<system.web>
<compilation debug="true" targetframework="4.0">

<trust level="Full" />
<httpruntime maxrequestlength="1048576"
="" executiontimeout="15000">

<customerrors mode="Off">


<pages enableEventValidation="true"/>
<!--<authentication mode="Forms">
<forms loginUrl="Registration.aspx" timeout="1300" />



<authorization>
<deny users="?">


-->

<sessionstate
="" mode="InProc" cookieless="true" timeout="1500" regenerateexpiredsessionid="true">


Nirav Prabtani 22-Mar-14 14:59pm    
<configuration>
<system.web>
<sessionstate timeout="20">

Neeraj_kaushik 22-Mar-14 15:02pm    
??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900