How can I execute a code after the loop has stopped?

advertisements

Hi is it possible to show a messagebox after 3 failed attempts for the pin to appear. Currently what i want my codes to do is, after 3 failed attempts, it will update a new random pin in the database. So when it has been updated, i want to show a messagebox that shows that a new pin has been sent. So this is my current code.

    string[] pinArray = new string[3];
    private void PinError()
    {

        for (int i = 0; i < pinArray.Length; i++)
        {

            if (pinArray[i] != null)
            {

                string strConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
                SqlConnection myConnect = new SqlConnection(strConnectionString);
                SqlCommand cmd = new SqlCommand("Update PostParcel set [email protected] where Box='1'and Pin!= '' ", myConnect);
                cmd.Parameters.AddWithValue("@pin1", RandomNumber(1000, 9999));
                myConnect.Open();
                cmd.ExecuteNonQuery();
                myConnect.Close();
            }
            else if (pinArray[i] == null)
            {
                pinArray[i] = txtpBox1.Text;

                MessageBox.Show("Invalid Pin. Pin Will Change After " + (3 - i) + " More Attempts");

                break;

            }

        }
    }

I have tried placing

MessageBox.Show("Invalid Pin. New Pin Has Been Sent.");
break;

in the 'if{}' section at the end. But trying the second failed attempt, it will show the messagebox "Invalid Pin. New Pin Has Been Sent.". Which made me realised that, on the second attempt, the 'if' will run. So technically, the pin would have been changed at the second attempt already. Is it possible to not let it change at the second attempt until the 3rd attempt and also to show a messagebox when the pin has been updated?

I have also tried adding

if (i == 2) continue;

into

if (pinArray[i] != null){  **if (i == 2) continue;** }

but on the second attempt it will just show that a new pin has been sent

P.s, sorry if this sorta question has been answered before. And thanks in advance for helping.

i kinda just did this

            if (pinArray[i] != null)
            {
                if (i == 2) continue;
                string strConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
                SqlConnection myConnect = new SqlConnection(strConnectionString);
                SqlCommand cmd = new SqlCommand("Update PostParcel set [email protected] where Box='1'and Pin!= '' ", myConnect);
                cmd.Parameters.AddWithValue("@pin1", RandomNumber(1000, 9999));
                myConnect.Open();
                cmd.ExecuteNonQuery();
                myConnect.Close();
                MessageBox.Show("Invalid Pin. New Pin Has Been Sent");
                break;

            }


Assuming you are calling PinError() for every wrong pin entered, try this below code

int i = 1;

private void PinError()
{
     if(i != 3)
     {
         MessageBox.Show("Invalid Pin. Pin Will Change After " + (3 - i) + " More Attempts");
         i++;
         return;
     }

     string strConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
     SqlConnection myConnect = new SqlConnection(strConnectionString);
     SqlCommand cmd = new SqlCommand("Update PostParcel set [email protected] where Box='1'and Pin!= '' ", myConnect);
     cmd.Parameters.AddWithValue("@pin1", RandomNumber(1000, 9999));
     myConnect.Open();
     cmd.ExecuteNonQuery();
     myConnect.Close();
     MessageBox.Show("Invalid Pin. New Pin Has Been Sent");
}