Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I am developing a on-line leave application in which I want to run a update query when user logs and the condition is:
C#
If(datetime today.month==6)
{
//execute some statement
}

but this will run every day for June
I want to run this code only once
Posted
Updated 21-Aug-11 20:02pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Aug-11 2:10am    
Fictional code? Why not writing code which can compile and using clipboard Paste to post any code?
--SA
walterhevedeich 22-Aug-11 2:10am    
What do you mean run once? Care to describe your scenario a little further? Provide some examples if necessary. Given a question like this, we will only be assuming things and in the end it might not be helpful to you.
Qureshali 22-Aug-11 2:23am    
Sir I mean that if the current month is June the I need to Update my Table. But if on 1st june if the user logs in the table should be updated, but if another say 5 june the user login that table should not be updated again
Philippe Mori 3-Sep-11 19:56pm    
Worst... what if the user does not log in for the whole month. Even the question is flawed.
BillWoodruff 4-Sep-11 3:19am    
I think Phillipe has given you an excellent answer, but I am curious to ask you one thing. It this a scenario where your application is running continuously ? Or, more likely, is it a scenario where the user launches and uses the application multiple times.

In terms of where you might store the date of the last update: look into .NET's 'Application.Settings' facility.

best, Bill

Try this:

C#
if(DateTime.Today.Month == 6 && DateTime.Today.Day == 1)
{
    //do the update 

}
 
Share this answer
 
Comments
Qureshali 22-Aug-11 23:48pm    
what if the user logs in on the other day.
For eg. the user logs in on 6 june than that condition will be false and the code will not run
Oludayo Alli 23-Aug-11 3:55am    
But you said: "if on 1st june if the user logs in the table should be updated, but if another say 5 june the user login that table should not be updated again". You're confusing me!
Philippe Mori 3-Sep-11 20:10pm    
Well, that solution would have work if the user log once per day... Your sampling code was misleading to start with.
You have to remember somewhere that the user has logged... In fact, the whole idea is flawed... What if the user does not log for the whole month of June, the action should probably be done next time he log...

You have to do something like :
- Get the last date the update query was run (hint: save also update date in a column of the table).
- Compute next time an update should be done.
- Check if the current date is greater that the date of the next update.
- If so, run the query (after updating modification date column)

Extra hint: All times should be in UTC.

Final advice: Try to do the code for checking date yourself. Uses a debugger if you need. Search Code Project if you have difficulties with dates. There is a lot of questions about that subject.
 
Share this answer
 
v3
Comments
RaisKazi 4-Sep-11 2:41am    
Perfect Answer 5!.
Tech Code Freak 4-Sep-11 11:09am    
Good one! 5up!
This code won't even compile, but the idea is clear. Why not combining several data comparison using Boolean operator "and" — "&&"?

—SA
 
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