How to calculate time duration between Logintime and Logouttime in asp.net c#

In this tutorial we have learn How to calculate time duration between Logintime and Logouttime in asp.net c#

In previous post we learnt How Pass session variable from page to page in asp.net. In this post we will learn some more things about session

In asp.net by default session timeout = 20 minutes, but in some cases we need to change session time in web.config file

Set various way session timeout :

  • Session Timeout using web.config file
  • Session timeout in each page using C# code
  • Session timeout using IIS Server for website

Web.config file

<system.web>
<sessionState mode=”InProc” timeout=”5″></sessionState>
</system.web>

C# code

Session.Timeout= 5;

Above line indicate that all session will be timed out after 5 minute if user doesn’t visit any new page of site.

Example of How to calculate time duration between Logintime and Logouttime in asp.net c#

Open visual studio and add new web form in website. Design web form with two button control, a label control and a Textbox control as show in below

 First, you create Class DAL.CS   this class called as Data Access Layer.

 namespace SESSionDemo
{
public class DAL
{
public int id { get; set; }
public string name { get; set; }
public string password { get; set; }
public string city { get; set; }

}
}

Create Class BAL.CS This class Called Busseness Access Layer and these class We can write the Query or Logic of database Query to fetch data

public class BAL
{
SqlConnection con = new SqlConnection(@”Data Source=.User Instance=True”);

public void insert_data(DAL da)
{
con.Open();
SqlCommand cmd = new SqlCommand(“insert into user_master values(‘” + da.name + “‘,'” + da.password + “‘,'” + da.city + “‘) “, con);
cmd.ExecuteNonQuery();
con.Close();
}

public DataTable login_data(DAL da)
{
DataTable dt = new DataTable();

SqlDataAdapter sda = new SqlDataAdapter(“select * from user_master where name='” + da.name + “‘and password='” + da.password + “‘”, con);

sda.Fill(dt);

return dt;
}
}

Now You can Add login.aspx page and add the Following code in Code Behind file.

namespace SESSionDemo
{
public partial class login : System.Web.UI.Page
{
DAL da = new DAL();
BAL ba = new BAL();
DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
da.name = TextBox1.Text;
da.password = TextBox2.Text;
dt = ba.login_data(da);
if (dt.Rows.Count > 0)
{
Session[“name”] = TextBox1.Text;
string abc = DateTime.Now.ToLongTimeString();
Session[“login_time”] = abc;

Response.Redirect(“home.aspx”);
}
}

When You click on login page the Validated user will redirect to Home page and display the user name and Login time for particular User Session as below code.

namespace SESSionDemo
{
public partial class home : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

string str = Session[“name”].ToString();
lbl_msg.Text=”Wel Come:” +str;
login_time.Text =Session[“login_time”].ToString();
}

protected void Button1_Click(object sender, EventArgs e)
{
System.Web.Security.FormsAuthentication.SignOut();

string logout = DateTime.Now.ToLongTimeString();
Session[“logout_time”] = logout;

Response.Redirect(“reg.aspx”);

}
}

How to calculate time duration between Logintime and Logouttime in asp.net c#

When User click on Logout button, it redirect to Reg.aspx page,and time span of Session can be calculate as following code

namespace SESSionDemo
{

public partial class Reg : System.Web.UI.Page
{

string result;

protected void Page_Load(object sender, EventArgs e)
{
lbl_logout.Text = Session[“logout_time”].ToString();

lbl_inmain.Text = Session[“login_time”].ToString();

DateTime logIn = DateTime.Parse(lbl_inmain.Text);

DateTime LOGOUT1 = DateTime.Parse(lbl_logout.Text);

TimeSpan result = LOGOUT1 – logIn;

lbl_deff.Text = result.ToString();

}
}

How to calculate time duration between Logintime and Logouttime in asp.net c#

SEE MORE

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *