Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
How to save logout time in database when user click on logout button using session in C#?
 string username = Session["uname"].ToString();
            string password = Session["pwd"].ToString();
            lbl_1.Text = username;
                cn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SP_TRACK_USE";
                cmd.Parameters.AddWithValue("@Username", username);
                cmd.Parameters.AddWithValue("@Password", password);
                cmd.Parameters.AddWithValue("@COMMAND", 1);
                cmd.ExecuteScalar();
                cn.Close();
                Response.Redirect("Default.aspx");
Coding runs properly, but logout time is not saved in database.
Posted 17-Dec-12 22:07pm
Edited 17-Dec-12 22:10pm
v2
Comments
faisal23 at 18-Dec-12 4:12am
   
In stored procedure pick the machine time by getDate() and on logout button call this procedure and at the same time insert into proper table.
Member 8110943 at 18-Dec-12 4:30am
   
i already try same coding as u said,but its not working.
AshishChaudha at 18-Dec-12 5:29am
   
have you tried debugging??? Please share your stored procedure.
Member 8110943 at 18-Dec-12 5:48am
   
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 

 
ALTER PROCEDURE [dbo].[SP_TRACK_USE]

@Username varchar(50),
@Password varchar(50),
@COMMAND INT = 0
AS
BEGIN
SET NOCOUNT ON;
IF(@COMMAND = 0)
BEGIN
INSERT INTO USERLOG (Username,Password,LOGIN,LOGOUT) VALUES (@Username,@Password,GETDATE(),NULL)
END
ELSE IF (@COMMAND = 1)
BEGIN
UPDATE USERLOG SET LOGOUT = GETDATE() WHERE Username=@Username and Password=@Password
END
END
 

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 

 
ALTER PROCEDURE [dbo].[SP_TRACK_USE]

@Username varchar(50),
@Password varchar(50),
@COMMAND INT = 0
AS
BEGIN
SET NOCOUNT ON;
IF(@COMMAND = 0)
BEGIN
INSERT INTO USERLOG (Username,Password,LOGIN,LOGOUT) VALUES (@Username,@Password,GETDATE(),NULL)
END
ELSE IF (@COMMAND = 1)
BEGIN
UPDATE USERLOG SET LOGOUT = GETDATE() WHERE Username=@Username and Password=@Password
END
END
 

 

 
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 

 
ALTER PROCEDURE [dbo].[SP_TRACK_USE]

@Username varchar(50),
@Password varchar(50),
@COMMAND INT = 0
AS
BEGIN
SET NOCOUNT ON;
IF(@COMMAND = 0)
BEGIN
INSERT INTO USERLOG (Username,Password,LOGIN,LOGOUT) VALUES (@Username,@Password,GETDATE(),NULL)
END
ELSE IF (@COMMAND = 1)
BEGIN
UPDATE USERLOG SET LOGOUT = GETDATE() WHERE Username=@Username and Password=@Password
END
END
 

 

 
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 

 
ALTER PROCEDURE [dbo].[SP_TRACK_USE]

@Username varchar(50),
@Password varchar(50),
@COMMAND INT = 0
AS
BEGIN
SET NOCOUNT ON;
IF(@COMMAND = 0)
BEGIN
INSERT INTO USERLOG (Username,Password,LOGIN,LOGOUT) VALUES (@Username,@Password,GETDATE(),NULL)
END
ELSE IF (@COMMAND = 1)
BEGIN
UPDATE USERLOG SET LOGOUT = GETDATE() WHERE Username=@Username and Password=@Password
END
END
 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 

 
ALTER PROCEDURE [dbo].[SP_TRACK_USE]

@Username varchar(50),
@Password varchar(50),
@COMMAND INT = 0
AS
BEGIN
SET NOCOUNT ON;
IF(@COMMAND = 0)
BEGIN
INSERT INTO USERLOG (Username,Password,LOGIN,LOGOUT) VALUES (@Username,@Password,GETDATE(),NULL)
END
ELSE IF (@COMMAND = 1)
BEGIN
UPDATE USERLOG SET LOGOUT = GETDATE() WHERE Username=@Username and Password=@Password
END
END
 

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 

 
ALTER PROCEDURE [dbo].[SP_TRACK_USE]

@Username varchar(50),
@Password varchar(50),
@COMMAND INT = 0
AS
BEGIN
SET NOCOUNT ON;
IF(@COMMAND = 0)
BEGIN
INSERT INTO USERLOG (Username,Password,LOGIN,LOGOUT) VALUES (@Username,@Password,GETDATE(),NULL)
END
ELSE IF (@COMMAND = 1)
BEGIN
UPDATE USERLOG SET LOGOUT = GETDATE() WHERE Username=@Username and Password=@Password
END
END
 

 

ALTER PROCEDURE [dbo].[SP_TRACK_USE]

@Username varchar(50),
@Password varchar(50),
@COMMAND INT = 0
AS
BEGIN
SET NOCOUNT ON;
IF(@COMMAND = 0)
BEGIN
INSERT INTO USERLOG (Username,Password,LOGIN,LOGOUT) VALUES (@Username,@Password,GETDATE(),NULL)
END
ELSE IF (@COMMAND = 1)
BEGIN
UPDATE USERLOG SET LOGOUT = GETDATE() WHERE Username=@Username and Password=@Password
END
END
 
please give me solution for this pbm.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why do you need to store the password ? Where are you storing the logout time ? If you add a column to store it and make the default getdate(), then that will work, but right now, you've either done that ( and it would work ), or you've not tried at all.
  Permalink  
Comments
Member 8110943 at 18-Dec-12 4:27am
   
UPDATE USERLOG SET LOGOUT = GETDATE() where Username=@Username and Password=@Password
i m trying this code for saving logout time.Any mistake then please guide me
Member 8110943 at 18-Dec-12 4:29am
   
UPDATE USERLOG SET LOGOUT = GETDATE()
i also try this code but it also not save the logout time in database..
Christian Graus at 18-Dec-12 14:20pm
   
Well, gee, you're trying things at random, do you have any clue what you're doing ? Perhaps if you told us what DOES happen, if you get an error, etc, that would help. Is logout a datetime ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

use SQL Server Session mode to store and pick the date from the system.
 
Exploring Session in ASP.NET[^]
hope it helped!
  Permalink  
Comments
Member 8110943 at 18-Dec-12 4:54am
   
Give me code for this..I tried but it doesn't work.
ProgramFOX at 18-Dec-12 5:20am
   
My Edit: Shouting removed.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
 
why not you alter the StoredProcedure with additional parameter 'logout_time' and pass the value
string username = Session["uname"].ToString();
            string password = Session["pwd"].ToString();
            lbl_1.Text = username;
                cn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SP_TRACK_USE";
                cmd.Parameters.AddWithValue("@Username", username);
                cmd.Parameters.AddWithValue("@Password", password);
                cmd.Parameters.AddWithValue("@logout_time", DateTime.Now());
                cmd.Parameters.AddWithValue("@COMMAND", 1);
                cmd.ExecuteScalar();
                cn.Close();
                Response.Redirect("Default.aspx");
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

You can do this in global.asax file.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 469
1 ChintanShukla 305
2 Sergey Alexandrovich Kryukov 253
3 RyanDev 250
4 Richard Deeming 250
0 Sergey Alexandrovich Kryukov 8,906
1 OriginalGriff 7,601
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,923


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100