Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends

Am working on .Net Platform as (Asp.net C#, SqlServer 2005)

am working on Project, in that i have a userslogin history details.

I took Gridview and displays UserLoginHistory as

UserId, Username, Login_Time, Logout_Time, Duration.



Now I need to Calculate the Login and Logout Time of Users login and Logout...and display in duration column.

such as... if the users access this application for 45 min....So, in duration column it must show 45 minutes of that particular accessed that appl.


Please help me,

Thanks u....!!
Posted

In C# it's easy:
C#
TimeSpan diff = logoutTime - loginTime;
Console.WriteLine(diff.TotalMinutes);

To return it in SQL:
SQL
SELECT DATEDIFF(mi, Logout_time, LoginTime) as Mins FROM UserLoginHistory
 
Share this answer
 
Comments
Ranjith Reddy CSE 25-Jun-12 6:02am    
Please i need it in ASP.NeTt, C#
OriginalGriff 25-Jun-12 6:22am    
And the code under "In c# it's easy:" is what, exactly?
Try below for calculating the duration.

SQL
SELECT DATEDIFF(hour, '2005-12-31 23:59:59.9999999'

, '2006-01-01 00:00:00.0000000');

SELECT DATEDIFF(minute, '2005-12-31 23:59:59.9999999'

, '2006-01-01 00:00:00.0000000');

SELECT DATEDIFF(second, '2005-12-31 23:59:59.9999999'

, '2006-01-01 00:00:00.0000000');


For more information use below link

http://msdn.microsoft.com/en-us/library/ms189794.aspx[^]
 
Share this answer
 
I think you can save the log-in and log-out time when user log-ins and log-outs respectively.
If you have these two time, then you can just calculate the difference between these times as the Duration.

Or else just explain in details about the problem.
Are you facing problem when saving these log-in and log-out time ?
 
Share this answer
 
Comments
Ranjith Reddy CSE 25-Jun-12 4:14am    
yes, am facing problem....Please can u show me...THANKS.
At the time of logging in the user, you can set the value of Log-in time to DB and at the time of log-out, just save the log-out time and also calculate the Duration.

At Log in -> -- Send the @UserId, @UserName to one procedure and write query
like below in the procedure.
Insert into UserLoginHistory (UserId, Username, Login_Time)
Values (@UserId, @UserName, Getdate())

At Log out -> -- Send the @UserId to one procedure and write query like below
in the procedure
Update UserLoginHistory
Set Logout_Time = Getdate() and Duration = (SELECT DATEDIFF(minute,
Login_Time, Getdate())
Where UserId = @UserId
Solution is depend on how you save login details in your database and how you handle logout process.
Note that there can be cases where user login and close the browser, do you have auto loggout when user not active for given time or some other way?
My idea is you can have insert record when user logged in to app, when user logout you can update same record with loggout time. in case of user not active for defined time duration we can consider that user as loggout.

when calculating durations you need to find users online and offline.
for online users you can calculate duration from login time to current time.
for offine users you can simply take last max login time and last max loggout time and take the duration.

Tracking users login/logout times on your site[^]
 
Share this answer
 
Comments
Ubaid ur Rahman IT 25-Jun-12 4:41am    
ya sure, you are right.

In case of user not active.....it should be logout automatically.

So how to do this automatically logout.

Please show me.
Ubaid ur Rahman IT 25-Jun-12 4:42am    
and also I have a problem, Generally Users login and close the browser....so how to do this.

Please help me my dear friends.

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