how to display username in all pages after successfully login

Posted by Ankitsrist under ASP.NET on 12/14/2012 | Points: 10 | Views : 63207 | Status : [Member] | Replies : 26
hello
i have a master page with various other pages including login page and i want to display username in all content pages after succesfully login.....
following is my code which i have used in login.aspx.cs page
protected void Button1_Click(object sender, EventArgs e)
{
mydb.myconnection();
SqlCommand cmd = new SqlCommand("insert into login(username, password) values ('"+TextBox1.Text+"', '"+TextBox2.Text+"')", mydb.sqlcon);
cmd.ExecuteNonQuery();
Session["username"] = TextBox1.Text;
Response.Redirect("welcome.aspx");


}

and followng code i have used in master.master.cs page load section
Label1.Text=Session["username"].ToString();


please help




Responses

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
and the error which m getting is "Object reference not set to an instance of an object." in master page

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
and the error which m getting is "Object reference not set to an instance of an object." in master page

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/14/2012 [Member] Bronze | Points: 25

Up
0
Down
in login page, on login button click if user is valied get user name and assign to the session
Session["Loginname"] = "username" ;


in master page load

if (Session["Loginname"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
lblName.Text = "Welcome:: " + Convert.ToString(Session["Loginname"]);
}

Mark if this helps you


Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 12/14/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Add this code on your Master page load
 

if (Session["Loginname"] != null)
lblName.Text = "Welcome:: " + Session["Loginname"].ToString();
else
Response.Redirect("Login.aspx");


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
sir it is giving an error "The page isn't redirecting properly" even i have given correct redirected page

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/14/2012 [Member] Bronze | Points: 25

Up
0
Down
if your session is null then it will be re-directed to the other page... so what is ur problem.. is the session not filled or you want to login to some other page

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dotnetrajanikanth on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
Firts check whether the login is successful or not, then pass it to session.

____________
www.flickr.com/photos/psdesigner/

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
i think the problem is session is not getting fill

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
@Dotnetrajanikanth sir i can login successfully....

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/14/2012 [Member] Bronze | Points: 25

Up
0
Down
have you assigned loginname to the session.


in master page load check whether the seesion is present

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
actualy this section creating problem
  if (Session["Loginname"] == null)

{
Response.Redirect("login.aspx");
}


when i removed this it is working properly


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dotnetrajanikanth on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
I have accomplished the same using the below code.

lbl.Text = Session("User").ToString()


I dont know C#. but this works fine with my system.

anyway for trail purpose jus do the following in your code.

Session["username"] = "Ankitsrist";


Do the above thing and check whether is it coming in all the pages or not. if it is coming then we can think of the rest.

____________
www.flickr.com/photos/psdesigner/

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
actually both sir Session["Loginname"] = "username" ; what is the use of username or ankitsrist from where it is getting this value


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
ignore previous comment

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
???????????????

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
my code for login page
mydb.myconnection();

SqlCommand cmd = new SqlCommand("insert into login(username, password) values ('"+TextBox1.Text+"', '"+TextBox2.Text+"')", mydb.sqlcon);
cmd.ExecuteNonQuery();
Session["Loginname"] ="username";


and for master page
protected void Page_Load(object sender, EventArgs e)

{

if (Session["Loginname"] == null)
{
Response.Redirect("login.aspx");
}
else
{
Label1.Text = "Welcome:: " + Convert.ToString(Session["Loginname"]);
}

}



Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 12/14/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Try this
 

string sqlString = "SELECT username FROM Login where username='" + TxtUsername.Text + "'and password='" + TxtPwd.Text + "'";
SqlDataAdapter da = new SqlDataAdapter(sqlString, conString);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
Session["Loginname"] = ds.Tables[0].Rows[0]["username"].ToString();
}
else
{
// Enter correct login details. -- MSG
}
}
else
{
// Error -- MSG.
}

Add this code on Master page Lode

if (Session["Loginname"] != null)
lblName.Text = "Welcome:: " + Session["Loginname"].ToString();
else
Response.Redirect("Login.aspx");


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dotnetrajanikanth on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
Ankitsrist,

It is just for checking whether you are able to assign anything to session.

____________
www.flickr.com/photos/psdesigner/

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
sir its now working i just changed response.redirect("login.aspx"); response.write("please login ") or just anything other than redirect...dont know y it was showing error......

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 12/14/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
can u plz share your MasterPage and Login Page ???


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dotnetrajanikanth on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
can u plz share your MasterPage and Login Page ???

____________
www.flickr.com/photos/psdesigner/

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/14/2012 [Member] Starter | Points: 25

Up
0
Down
master page--------
protected void Page_Load(object sender, EventArgs e)

{


if (Session["Loginname"] != null)
{

Label1 .Text = "Welcome : " + Session["Loginname"];
}
else
{
Response.Write("please login");
}

}
protected void Button1_Click(object sender, EventArgs e)
{

Session["Loginname"] = null;
Response.Redirect("logout.aspx");
}


login page-----------
protected void Button1_Click(object sender, EventArgs e)

{
mydb.myconnection();
SqlCommand cmd = new SqlCommand("insert into login(username, password) values ('"+TextBox1.Text+"', '"+TextBox2.Text+"')", mydb.sqlcon);
cmd.ExecuteNonQuery();
Session["Loginname"] =TextBox1 .Text ;
Response.Redirect("welcome.aspx");



}


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 12/14/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
@Ankitsrist
i send u attached file of Login page plz find attached file .
 Download source file

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/26/2012 [Member] Starter | Points: 25

Up
0
Down
thanks ranjeet :)

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/26/2012 [Member] Starter | Points: 25

Up
0
Down
thanks ranjeet :)

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response