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
MessageBox.Showstops 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 checkingseconds < 1rather than== 0, producing more messageboxes, and eventual SQL calls.Rather than
I would suggest you
Stopping the timer first ensures that the
seconds < 1branch should only ever happen once (although I’d still check== 0instead, and possibly change yourelsetoelse if (seconds > 0); running the SQL second ensures that, by the time the message is shown, the user’s card has indeed been confiscated.