0

I have a page created in ASP.NET with a login page. I have made an option to activate something after logging in. This value is by default set to "False" in SQL database. When the user clicks on activate button then this column is updated to "True" using a query in C#.

My problem is that after updating the value to "True", after 1 hour this column should be automatically updated as "False" even if the user is logged in or not. How can I do this using C# and SQL?

2
  • are you implementing something similar to session expire time, which can be configured by web server itself? Commented May 11, 2017 at 6:20
  • Actually i am doing a ticket booking application. An user will make a request to book an ticket. The admin will login and can view all the request. The admin will click activate for a particular request and this will stay activated for one hour. So within this 1 hour the admin have to book ticket and upload the ticket details. If 1 hour passes then the activated request will become false again. Commented May 11, 2017 at 6:40

3 Answers 3

3

The simplest solution would be to change the data type to datetime, and set it's value to the last time the user clicked the activate button. Then you don't need to automatically update it later on, you only need to check if it's value is within the last hour.

Sign up to request clarification or add additional context in comments.

2 Comments

But i dont want to check the time and update it. For that i would need an event that i should trigger right? I want to update the SQL data automatically. Like setting up a procedure or something during the time of activation. Then after 1 hour it will run automatically
Yeah, I've got that. But what you are asking is how to implement a solution that is wrong to begin with. You are required to raise a flag and keep it up for one hour. Now, there are several ways to do it, and almost all of them requires saving the date and time when that particular flag was raised. What I'm suggesting is to use only that date and time and saves you the need to worry about putting the flag back down. That's why it's the simplest solution. Check out Kumar's answer to see what I mean.
1

Alter table to add one more column called ActiveDate datetime

whenever that column is true or false,simply set the current time in ActiveDate

Now Create a Scheduler. My example consist of window schedular which run every 1 hour

Make a programme in console application that you would simply invoke your proc.

This proc make all true to false.

Convert this console to exe and attach to Window Schedular.

2 Comments

I think the scheduler should run every minute. If you activated at 13:15 you want the timeout at 14:15 not 14:00 I assume.
@MarkusDeibel,sorry i forgot this scenario.running scheduler every minute=keep on running scheduler. Then I think i will go with Zohar Peled solution.
0

Simply Follow The Steps Here You just need to change the query According to your need and set the schedule at every 1 hour

or you can create trigger on update and add this code for delay of an hour

DECLARE @MyDateTime DATETIME
SET @MyDateTime = DATEADD(HOUR,1,GETDATE())
WAITFOR TIME @MyDateTime
SELECT GETDATE() CurrentTime

10 Comments

As I mentioned below setting the schedule to one hour will not yield the required timeout of one hour. It will give a time out between 1s and 59:59 depending on when the user clicks activate. You'll need to run it every minute or second depending on the required granularity.
@MarkusDeibel I have added another link you can follow this one
The link you added points to an answer for MySQL while the OP references SQL-Server. So this won't help much in the solution.
This will make the entier update statement wait for an hour. Probably not the best idea.
Yes I also have tested and its not executing the other query until trigger executed completely.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.