How To Calculate Session Time Duration Login to LogOut in Asp.Net
Introduction : This Tutorial Explain How to use Session in asp.net. some times it can be need to calculate the time duration of the session then you need to time span of login to logout of user and this tutorial can display fully session information with implemented project code in asp.net with 3-Tier Architecture.
Download Complete Code Here : Download
Download Complete Code Here : Download
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=.\SQLEXPRESS;AttachDbFilename=D:\program\SESSionDemo\SESSionDemo\App_Data\db1.mdf;Integrated Security=True;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 Valid user van redirect to Home page and display the user name and Login time of the User Session.
When user can redirect to home page home page can load and display Login time and User name in the following label
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");
}
}
When User can Logout from system it display the logout time in 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();
}
}
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=.\SQLEXPRESS;AttachDbFilename=D:\program\SESSionDemo\SESSionDemo\App_Data\db1.mdf;Integrated Security=True;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 Valid user van redirect to Home page and display the user name and Login time of the User Session.
When user can redirect to home page home page can load and display Login time and User name in the following label
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");
}
}
When User can Logout from system it display the logout time in 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();
}
}
In label lbl_logout & lbl_inmain it can be need to parse with new label because we need to calculate the time span for time duration,
See Other Tutorial :
* AngularJS CRUD Operation in ASP.Net MVC
* AngularJS With ASP.NET MVC
* Convert Rows to columns using 'Pivot' in SQL Server
* Mvc Registration page With user exist using Ajax method
* MVC 4 How to Perform Insert Update Delete Edit Select Operation
* MVC4 Edit,update,Delete,cancel inside gridview using sql database
* MVC 4 Gridview To Display Data Using SQL Server Database With Simple code
* Login page in asp.net Mvc4 Web application
* Mvc4 How to bind Dropdown List using Sql Database
* Gridview find control in asp.net
See Other Tutorial :
* AngularJS CRUD Operation in ASP.Net MVC
* AngularJS With ASP.NET MVC
* Convert Rows to columns using 'Pivot' in SQL Server
* Mvc Registration page With user exist using Ajax method
* MVC 4 How to Perform Insert Update Delete Edit Select Operation
* MVC4 Edit,update,Delete,cancel inside gridview using sql database
* MVC 4 Gridview To Display Data Using SQL Server Database With Simple code
* Login page in asp.net Mvc4 Web application
* Mvc4 How to bind Dropdown List using Sql Database
* Gridview find control in asp.net
Sign up here with your email
1 comments:
Write commentsGreate article. Keep wrifing such kind of
Replyinformation on your blog. Im really impressed by it.
Hey there, You have performed a great job. I'll certainly diggg itt and
individually recommend to my friends.I am confident they will be benefited frim tuis
website.
ConversionConversion EmoticonEmoticon