Click here to Skip to main content
15,041,525 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi friends,

i want store user login and logout time in the database.

after user login, logout time is automatically updated after 10 min.

i m using asp.net c# and back-end sql server 2005.

how can i do it.

plz help me....
Posted
Updated 4-Oct-11 19:59pm
v2
Comments
Member 9641223 29-Nov-12 4:49am
   
i am created one website in log in page when ever user entered log in details and click log in button i want to save that time and in home page one log out link when ever user clicks that link i want to save that log out time please help me
Member 13535686 28-Jan-18 2:23am
   
how to apply solution 3 on asp.net

SQL
CREATE TABLE USERLOG(
   ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
   USERID INT,
   LOGIN DATETIME,
   LOGOUT DATETIME
)

SQL
CREATE PROCEDURE SP_TRACK_USERLOG
	@USERID INT = NULL,
	@COMMAND INT = 0
AS
BEGIN
	SET NOCOUNT ON;
	IF(@COMMAND = 0)
		BEGIN
			INSERT INTO USERLOG (USERID,LOGIN,LOGOUT) VALUES (@USERID,GETDATE(),NULL)
		END
	ELSE IF (@COMMAND = 1)
		BEGIN
			UPDATE USERLOG SET LOGOUT = GETDATE() WHERE USERID = @USERID
		END
END


C#
SqlConnection cs = new SqlConnection("YOUR CONNECTION STRING");
cs.Open();
SqlCommand cmd = new SqlCommand();
public void Login(int userID)
{            
   cmd.Connection = cs;
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.CommandText = "SP_TRACK_USERLOG";
   cmd.Parameters.AddWithValue("@USERID", userID);
   cmd.Parameters.AddWithValue("@COMMAND", 0);
   cmd.ExecuteNonQuery();
}
public void LogOut(int userID)
{
   cmd.Connection = cs;
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.CommandText = "SP_TRACK_USERLOG";
   cmd.Parameters.AddWithValue("@USERID", userID);
   cmd.Parameters.AddWithValue("@COMMAND", 1);
   cmd.ExecuteNonQuery();
}
   
v2
Comments
Member 9150919 21-Jun-12 6:01am
   
please pls say about the design sir
Take the value of System.DateTime.Now after successful login & logout events and store it into database.
Do you need anything else to be done?
   
Comments
Member 11568559 18-Oct-16 9:32am
   
I would like to store System login & logout timings in database, by using C# code. Can you please help me
Hi,

use session_start and session_end events.

In Session_start event write code for login in DB table
In session_end event write code for logout in DB Table

Try yourself some code If there are any error post your question with code

All the Best
   
Comments
Rajendra Patel Jat 9-Feb-12 8:17am
   
nice
   
Comments
Rajendra Patel Jat 10-Feb-12 8:44am
   
hi
Rajendra Patel Jat 10-Feb-12 8:46am
   
any body help me please i have one task my application whenever user login account then signout after user login previous login date display how please help me
Yes it is so simple when u login update tabel logincolumn as well as logout...
for it you will have to get system value and can do it....
   
yes, session is right for this query.....
   

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900