How to store login and logout time in asp.net
i have two table one is login table and second is logdetails.
on each login i am able to add login time and log id to logdetails table but i am not able to add logout time.
how can i solve this.
below is the code where logout time is not getting added.
CREATE TABLE [dbo].[Login](
[logId] [int] IDENTITY(1,1) NOT NULL,
[uname] [varchar](20) NULL,
[upasswd] [varchar](20) NULL,
}
CREATE TABLE [dbo].[LogDetails](
[LogDetID] [int] IDENTITY(1,1) NOT NULL,
[logId] [int] NULL,
[LogInime] [datetime] NULL,
[LogOutime] [datetime] NULL
}
ALTER procedure [dbo].[login_1ba]
(
@uname NVARCHAR(20)=NULL,
@upasswd NVARCHAR(20)=NULL,
@action int
)
as
begin
set nocount on;
declare @status int,@logId INT, @Logtime datetime=GETDATE(),@LogDetID INT
if(@action=0)
begin
if exists(select * from Login where uname=@uname and upasswd=@upasswd)
begin
SET @logId = (SELECT logId FROM Login where uname=@uname and upasswd=@upasswd)
insert into LogDetails(logId,LogInime,LogOutime) values(@logId,@Logtime,null)
set @status=1
end
else
begin
set @status=0
end
select @status
end
else if(@action=1)
begin
set @LogDetID=(SELECT MAX(LogDetID) AS LastID FROM LogDetails where AdmlogId in(SELECT logId FROM Login where uname=@uname))
UPDATE LogDetails SET LogOutime = GETDATE() WHERE LogDetID = @LogDetID
end
end
private void usersessionstart()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
SqlDataAdapter ada = new SqlDataAdapter();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("login", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@uname", txtusername.Text);
cmd.Parameters.AddWithValue("@upasswd", txtpassword.Text);
cmd.Parameters.AddWithValue("@action", 0);
con.Open();
int status;
status = Convert.ToInt16(cmd.ExecuteScalar());
if (status == 1)
{
FormsAuthentication.RedirectFromLoginPage(txtusername.Text, false);
Session["uname"] = txtusername.Text;
string uname = Session["uname"].ToString();
usersessionend(uname);
}
else
{
Label1.Text = "invalid login";
}
}
private void usersessionend(string uname)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("login", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@uname", uname);
cmd.Parameters.AddWithValue("@action", 1);
cmd.ExecuteNonQuery();
Session["uname"] = null;
Session.Remove("uname");
Session.Abandon();
Session.RemoveAll();
}
protected void btnlogin_Click(object sender, EventArgs e)
{
usersessionstart();
}
protected void ctlLogin_LoggingOut(object sender, LoginCancelEventArgs e)
{
usersessionend(Session["uname"].ToString());
}