Click here to Skip to main content
15,029,501 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
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
Comments
AshishChaudha 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 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 21-Dec-12 9:09am
   
Did you invoke Session.Abandon() method after completion all of the logic in logout button ??

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
   
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.
   

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