Reputation: 2528
I would like to stop people from constantly syncing/updating items in the DB as this could cause potential problems. I have a time the last sync/update occured which is stored in the DB. What I need to do is disable the user from initiating another update/sync until 15 minutes has passed since the last update/sync.
How would i go about doing this?
Update: for some reason i am getting the error "A new expression requires (), [], or {} after type" on DateTime lastUpdate. Any ideas why this could be?
DataDataContext dc = new DataDataContext();
DateTime lastUpdate = from t in dc.Settings
where t.id == 1
select t.lastSync;
if ((DateTime.Now - lastUpdate).TotalMinutes >= 15)
{
}
else { }
Update: Sorted, i missed off the (); of the datacontext!! facepalm
Final Update: All fixed and working! Many thanks for all your help, for the ones who think this "smells fishy" or "is bad" then here is what i did!!
DateTime lastUpdate = (from t in dc.Settings
where t.id == 1
select t.lastSync).Single();
if ((DateTime.Now - lastUpdate).TotalMinutes >= 15)
{
syncbuttons.Visible = false;
}
else { syncbuttons.Visible = true; }
Now please explain what is so suspect about what i am trying to do? Stopping users from hammering a database? What if i have 30 users attempting to update/sync. Would not be that good would it!
Upvotes: 0
Views: 102
Reputation: 978
The problem that causes the error is that your linq expression returns an (IQuerryable) collecton, but you are trying to assign/cast it to a DateTime.
Try using
DateTime lastUpdate = (from t in dc.Settings
where t.id == 1
select t.lastSync).Single();
Regarding the Db sync; I'm no DB expert, but I don't think that 30 connections should be considered anywhere near a heavy load. If it is then you are much better of if you optimize the the db and transactions.
Disabling a Sync-button won't stop users from hitting the reload-button, which in turn probably will generate a lot more traffic than a custom tailored refresh procedure.
And even if you fix that, what if your user-base increases to a point where you get 30 connections a second without people hammering on a sync button.
So my suggestion is that you go back and see if you can minimize the impact of the syncing instead. One way might be to put a timestamp on the records that you are fetching. Than index that column so it will be fast on lookups and have the db check if any rows are newer than the users las sync.
Just making your application unresponsive or lagging (in the eyes of the user) should not be considered a viable solution.
At least that's how I would approach the problem.
Upvotes: 1
Reputation: 43743
DateTime lastUpdate = ...; // Retrieve from DB
TimeSpan elapsed = DateTime.Now - lastUpdate;
if (elapsed.TotalMinutes >= 15)
{
}
Or, in one line:
if ((DateTime.Now - lastUpdate).TotalMinutes >= 15)
Upvotes: 0