Reputation: 55
Hi guys have an issue with the timer basically i have the seconds variable set to 60 and when its counted down to 0 i want my SQL command to run to change the database and then for it to produce a messagebox saying ''card confiscated'' or something then once they click ok the application to stop
private void timer1_Tick(object sender, EventArgs e)
{
if (seconds < 1)
{
MessageBox.Show("Option timer Expired Card Confiscated please contact your local branch");
timer1.Enabled = false;
sqlCommandTimer.Parameters["@cardNum"].Value = Class1.cardNumber;
sqlCommandTimer.Parameters["@confiscated"].Value = true;
try
{
sqlCommandTimer.Connection.Open();
sqlCommandTimer.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Application.Exit();
sqlCommandTimer.Connection.Close();
}
}
else
{
seconds--;
listBox2.Items.Add(seconds);
}
this is my SQL command
UPDATE dbo.ATMCards
SET confiscated = @confiscated
WHERE (cardNumber = @cardNum)
any help on what im missing would be greatly appreciated :) thanks
edit: oh crap sorry forgot to add my problem it goes to 0 then basically continually spams the message
Upvotes: 1
Views: 228
Reputation: 50104
MessageBox.Show
stops the rest of your code being executed until the user clicks OK/Cancel/whatever, but the timer will keep on ticking and, since you are checking seconds < 1
rather than == 0
, producing more messageboxes, and eventual SQL calls.
Rather than
I would suggest you
Stopping the timer first ensures that the seconds < 1
branch should only ever happen once (although I'd still check == 0
instead, and possibly change your else
to else if (seconds > 0)
; running the SQL second ensures that, by the time the message is shown, the user's card has indeed been confiscated.
Upvotes: 1