Free Jobs

Dot Net Forums: ASPNET (77) C#.NET (39) VB.NET (8) AJAX (21) Silver Light (1) WCF (10) WPF (1) WWF (1) .NET 3.5 (3) SQL SERVER (38) Other (3)

Topic: How to use Session in ASP.NET C# for user Login Logout status

To get the Posted Questions details daily in your mail Click Here

Share this Post on Facebook/Twitter/Orkut Bookmark and Share

User: dhiraj5079 Post Date: 03 Dec 2010 08:08

For tracking and showing user logged in and logged out status one of the
best way is to use Session Variable.In this post I will discuss the same.

Main Content:
Suppose we have stored all the username password in a table

Inside Login.aspx.cs page ,In the ButtonLogin_Click write the belwo code.
Your actual code may differ ,This is just for understanding purpose.

//This function takes out all the users table data from database and
//the username password combination.
protected void ButtonLogin_Click(object sender, EventArgs e)
//This code is to check the username password of user.
string username = TextBoxUserName.Text;
string password = TextBoxPassword.Text;

bool matched = false;

//Calling the method of GetAllUsersData of business access layer class(bal)

DataSet dsUsers = balObject.GetAllUsersData();
int noOfRows = dsUsers.Tables["UsersTable"].Rows.Count;

for(int i = 0 ;i {
//Comparing for correct username password combination.
//If both username and password in the same row match with the
corresponding username password
//entered in the text box at login page the matched is set
true . other wise match remains false
if ((dsUsers.Tables["UsersTable"].Rows[i]["username"].ToString() ==
username) && (dsUsers.Tables["UsersTable"].Rows[i]["password"].ToString()
== password))
matched = true;
Session["LoggedIn"] = "correct";


Inside the HomePage.aspx.cs Page_Load Method write the following code

if (Session["LoggedIn"] != null)

if (Session["LoggedIn"].ToString() == "correct")
ButtonLogin.Text = "Logout";

ButtonLogin.Text = "Login";


When Clicked on the Logout button
You should must set the Session["LoggedIn"] to null as below

protected void ButtonLogout_Click(object sender, EventArgs e)
Session["LoggedIn"] = null;

In this way you can use Session Variable to Check User Status

About Writer:
Hi,I am IT Professional From Mumbai

Writer's Link: http://

Writer's Email:

  • User: Post Date:   05 Nov 2011 02:04
    sir,i need job in .net around delhi(NCR),i have done MCA(60%) ,with six months industrial training in .net from Cmc ltd noida,pls help me..


Post Comment

Similar Articles Top ArticlesLatest Articles Other Articles by this Author Copyright © 2009-2011 | All Rights Reserved
All content of this website is protected by copyright laws and should not be reproduced in any form or by any means without written permission
(Best viewed in IE 8.0+ or Firefox 3.0+ at resolution of 1024 * 768 or higher)