Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more: , +
Hi to all,

I m working in SQL server 2008 and asp.net platform. My project has a button which on being clicked, shows the login time of the user.
I need a code as such that when the user logs in for the second time on the same day we have to display a label showing "You have logged in at" and a textbox displaying the first login time for that day. Also the button should be disabled on the page.

Any help regarding this would be very useful.

Thank you.
Posted
Comments
[no name] 28-Aug-12 8:18am    
Respond me, if u need query for that....
prabhadevi chennai 28-Aug-12 8:47am    
Thank you for your response,

I need to calculate the time difference between login and logout too. I want to display the first login of the current date after which the button should be disabled for all the successive logins on that particular date by the user.

Can you suggest me how to perform the above operations with the query?

You should have Logindetails table.

You can do this in 2 way
First way

Insert new record everytime the user logins and get the min(time ) for that particular day.
This is kind of history so you can keep track of every login

Second way:

Insert new record when user logins first time for the day. If user tries to login second time, go and check whether a record already exists for that particular day and for particular user . If it exists dont insert.
So now you will have only one login time per day per user. No history tracking.

SQL
if not exists(select 1 from  logintable
where CONVERT(varchar(8), logindate, 112) =CONVERT(varchar(8), GETDATE(), 112) and Id=1)
insert into logintable(col1,col2,col3,logindate) values (val1,val2,val3,getdate())


updated solution, based on the comments.
C#
protected void Button2_Click(object sender, EventArgs e)
    {
        string intime = DateTime.Now.ToString();

        string ID = Session["Name"].ToString();
        string indate = DateTime.Now.ToShortDateString();
        SqlConnection cn = new SqlConnection("Data Source=Sony-VAIO\\MSSQLSERVER2008;InitialCatalog=db;Integrated Security=True");
        cn.Open();
        SqlCommand cmd = new SqlCommand(@"if not exists (select 1 from intime where convert(varchar(30),indate,112)=convert(varchar(30),getdate(),112) and Id=@Id) insert into intime
values ('" + ID + "','" + intime + "','" + indate + "')", cn);
        cmd.Parameters.AddWithValue("@Id", ID);

        cmd.ExecuteNonQuery();
        cn.Close();


    }
 
Share this answer
 
v4
Comments
Kuthuparakkal 28-Aug-12 8:43am    
i agree... nice reply.. my 5+
prabhadevi chennai 28-Aug-12 8:50am    
Thank you for your response,
Can you suggest a query for your second option?
Santhosh Kumar Jayaraman 28-Aug-12 9:01am    
Check my updated solution., here am checking whether any record exists for currentdate if no, then am inserting into logintable.

Logintable=tablename
logindate=column..So you have to replace normal insert things which you already have to this in stored proc.
So at a time there will be only one record for user/day
prabhadevi chennai 28-Aug-12 9:43am    
Sir, When i give the above query every time it is getting inserted as it is not checking with the current date. Also it displays the current date and time too.
Santhosh Kumar Jayaraman 28-Aug-12 10:57am    
CONVERT(varchar(8), logindate, 112) =CONVERT(varchar(8), GETDATE(), 112) ) it will get only date and am comparing date only not time.
step 1:
update ur table, logtime column on every login

step 2:
compare that date with the current date,
if it matches retrive date and time and show it in textbox
also disable the button
otherwise,
retrieve nothing
 
Share this answer
 
You can store the first login time in a database.
Here are some examples -
Tracking users login/logout times on your site[^]
http://www.4guysfromrolla.com/articles/081507-1.aspx[^]
 
Share this answer
 

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