Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
I want to save the login and logout time in current date within single row. Could any one share the coding in C# that will help me.
Posted 21-Dec-12 0:13am
Comments
AshishChaudha at 21-Dec-12 6:20am
   
have you tried anything? dont expect from anybody to write code for you. here all are help if some one getting problem or got stucked..
Member 8110943 at 21-Dec-12 6:54am
   
Logout button:-
 
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");
 
and the store procedure for this code :-
 
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
 
I want that when user click the logout button then it redirect on login page.
My code redirect to login page without any error but not save the logout time in database.
Oleksandr Kulchytskyi at 21-Dec-12 9:09am
   
Did you invoke Session.Abandon() method after completion all of the logic in logout button ??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello Member 8110943,
 
Isn't the problem that when the user is redirected to the login page for the logout the login gets the log out time again?
 
Anyway, I'd suggest that you simply use two different pages, just add one for the log out.
 
Cheers,
Edo
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You may find some anomalies in your data because not everyone will log out through your site. Most logouts will occur when uses simply navigate away from your site or close their browser. In these cases, you won't get an explicit logout that you can apply to your system.
However, the basic logic would be to create the login row in your database when they login leaving the logout field with a value of NULL.(I'm assuming you already have something like that from your question)
On logout, I would suggest that you just need to pull the latest login record for the current user where the logout date is NULL and simply write the current time to that record. If no record is found, then just ignore the logout request and assume the user isn't logged in at all.
But please don't ignore the first part of my answer. There are many things that will happen to mess up your data if you don't write something to clean up these rogue records.
  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 405
1 Maciej Los 285
2 Peter Leow 128
3 Kornfeld Eliyahu Peter 119
4 DamithSL 119


Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid