Hi having abit of a problem with my code it refuses to do my else statement have a global variable set to say attempts = 3
runs the while loop but when i put an invalid pin it basically just doesnt run anything i would appreciate any help possible thanks
while (read.Read())
{
if (read.HasRows)
{
}
//If the card isnt confiscated then do this:
if (((Boolean)(read["confiscated"]) == false))
{
string cardnum = read["cardNumber"].ToString();
string pinnum = read["PIN"].ToString();
//Compare the results in the table against those put in by the customer
if (string.Equals(cardnum, cardBox.Text) && string.Equals(pinnum, pinNumber.Text))
{
MessageBox.Show("Welcome customer");
MessageBox.Show("Card Number " + cardnum + " PIN " + pinnum);
//if the login details match and everything is correct then bring the next form up
MessageBox.Show("Details are correct");
pinNumber.Clear();
//open the options form
Form optionForm = new optionForm();
optionForm.Show();
//hide this form
this.Hide();
break;
}
else
{
if (attempts == 1)
{
sqlCommandATM.Parameters["@cardNum"].Value = cardBox.Text;
sqlCommandATM.Parameters["@confiscated"].Value = true;
MessageBox.Show("Your card has been confiscated please contact The bank of Glamorgan to resolve the issue");
pinNumber.Clear();
}
EDIT
I’ve tried to understand what you’re doing. I assume you’re trying to authenticate a person by credit card number and PIN against a database, and you’re doing it by getting all the entries from a database table and looping over them to find the one that matches the user input. You want to allow the user to do that three times before locking the credit card.
I see more than one issue with that: The task of finding the correct row in the table for the user should be left to the database. This is much quicker and less error prone. So if you’d like to do it right, then you should have an SQL statement like:
If you really insist on doing it your way, then please make sure that
attemptsis initialized properly and is incremented on each login attempt – in your current code, you’d increment it on every row you’re validating against, so you’d lock out every customer who’s not within the first three rows you’re getting from the database.Original reply
OK, you’re reading all data from a DataReader in a
whileloop:Then what do the following three lines do? You wouldn’t be here if
read.HasRowswasfalse, becauseread.Read()would have returnedfalsealready. The following three lines are pointless and can be removed, leaving only what’s inside the block.The usual way would be:
More is hard to tell as you’re not showing us the complete code – what value does
attemptshave before thewhileloop, for example?