locked
Windows Forms Enter code and reset failed attempts RRS feed

  • Question

  • Hi there,
    I still have a task that I still have to do for school. Unfortunately I have never done this task before. I hope someone can help me with that.

    Task:
    If an incorrect username or password is entered three times in succession, the GUI under 4. is supplemented by a label "lbl_code" and a text field "txt_code". It is no longer possible to enter the user name and password. After entering the code "0815", the GUI shows the basic state again and the failed attempts are deleted.

    My program:

    namespace Projektarbeit_Pförtneranlage
    {
        public partial class FormLogin : Form
        {
            static int attempt = 3;
            public FormLogin()
            {
                InitializeComponent();
            }
            //Verbindungszeichenfolge
            string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\patri\Downloads\LoginApplicationSourceCode\LoginApplicationSourceCode\LoginApplication\LoginApplication\MyDatabase.mdf;Integrated Security=True;Connect Timeout=30;";

            private void button2_Click(object sender, EventArgs e)
            {
                string username = txt_name.Text;
                string password = txt_pwd.Text;

                if (txt_name.Text == "" || txt_pwd.Text == "")
                {
                    attempt = 0;

                    MessageBox.Show("Bitte geben Sie Benutzername und Passwort ein!");
                    return;
                }
                else if ((attempt == 3) && (attempt > 0))
                {
                    lbl_versuch.Text = ("Sie haben nur noch " + Convert.ToString(attempt) + " Versuche.");
                    --attempt;
                }
                else if ((attempt == 2) && (attempt > 0))
                {
                    lbl_versuch.Text = ("Sie haben nur noch " + Convert.ToString(attempt) + " Versuche.");
                    --attempt;
                }
                else if ((attempt == 1) && (attempt > 0))
                {
                    lbl_versuch.Text = ("Sie haben nur noch " + Convert.ToString(attempt) + " Versuch.");
                    --attempt;
                }
                
                try
                {
                    //Erstellen Sie SqlConnection
                    SqlConnection con = new SqlConnection(cs);
                    SqlCommand cmd = new SqlCommand("Select * from tbl_Login where UserName=@username and Password=@password", con);
                    cmd.Parameters.AddWithValue("@username", txt_name.Text);
                    cmd.Parameters.AddWithValue("@password", txt_pwd.Text);
                    con.Open();
                    SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adapt.Fill(ds);
                    con.Close();
                    int count = ds.Tables[0].Rows.Count;
                    //Wenn count gleich 1 ist, wird die Form frmMain angezeigt
                    if (count == 1)
                    {
                        MessageBox.Show("Anmeldung erfolgreich!");
                        this.Hide();
                        FormGUI fm = new FormGUI();
                        fm.Show();
                    }
                    else
                    {
                        MessageBox.Show("Login Failed!");

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
    • Edited by Patrick96 Thursday, February 13, 2020 7:16 PM
    Thursday, February 13, 2020 7:09 PM

All replies

  • Hi Patrick96,
    Base on your description, it is recommend to run your project and find where the error you encountered?
    And I have a suggestion you can refer to.
    First, you can pop up the GUI form When entered for the fourth time.

    else if (attempt == 0)
    {
        Form2 f2 = new Form2();
        f2.ShowDialog(this);
    
    }

    Then you can change the value of attempt to 3 in GUI form and set label.text to show the ""It is no longer possible to enter the user name and password".

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text == "0815")
         {
            Form1 f1 = (Form1)this.Owner;
            f1.attempt = 3;
            this.Hide();
        } 
    }

    Hope it is helpful for you.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 14, 2020 6:38 AM
  • thanks for his answer. I have inserted your examples in the program. Unfortunately, an error message shows:
    Error CS0176: The FormLogin.attempt member cannot be accessed with an instance reference. Instead, qualify it with a type name.

    Here I have marked the error in bold and sub lines.

     private void txt_code_TextChanged(object sender, EventArgs e)
            {
                if (txt_code.Text == "0815")
                {
                    FormLogin f1 = (FormLogin)this.Owner;
                    f1.attempt = 3;
                    this.Hide();
                }
            }
    My program:
    namespace Projektarbeit_Pförtneranlage
    {
        public partial class FormLogin : Form
        {
            static int attempt = 3;
            public FormLogin()
            {
                InitializeComponent();
            }
            //Verbindungszeichenfolge
            string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\patri\Downloads\LoginApplicationSourceCode\LoginApplicationSourceCode\LoginApplication\LoginApplication\MyDatabase.mdf;Integrated Security=True;Connect Timeout=30;";
    
            private void button2_Click(object sender, EventArgs e)
            {
                string username = txt_name.Text;
                string password = txt_pwd.Text;
    
                if (txt_name.Text == "" || txt_pwd.Text == "")
                {
                    attempt = 0;
    
                    MessageBox.Show("Bitte geben Sie Benutzername und Passwort ein!");
                    return;
                }
                else if ((attempt == 3) && (attempt > 0))
                {
                    lbl_versuch.Text = ("Sie haben nur noch " + Convert.ToString(attempt) + " Versuche.");
                    --attempt;
                }
                else if ((attempt == 2) && (attempt > 0))
                {
                    lbl_versuch.Text = ("Sie haben nur noch " + Convert.ToString(attempt) + " Versuche.");
                    --attempt;
                }
                else if ((attempt == 1) && (attempt > 0))
                {
                    lbl_versuch.Text = ("Sie haben nur noch " + Convert.ToString(attempt) + " Versuch.");
                    --attempt;
                }
                else if (attempt == 0)
                {
                    FormLogin f2 = new FormLogin();
                    f2.ShowDialog(this);
    
                }
    
                try
                {
                    //Erstellen Sie SqlConnection
                    SqlConnection con = new SqlConnection(cs);
                    SqlCommand cmd = new SqlCommand("Select * from tbl_Login where UserName=@username and Password=@password", con);
                    cmd.Parameters.AddWithValue("@username", txt_name.Text);
                    cmd.Parameters.AddWithValue("@password", txt_pwd.Text);
                    con.Open();
                    SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adapt.Fill(ds);
                    con.Close();
                    int count = ds.Tables[0].Rows.Count;
                    //Wenn count gleich 1 ist, wird die Form frmMain angezeigt
                    if (count == 1)
                    {
                        MessageBox.Show("Anmeldung erfolgreich!");
                        this.Hide();
                        FormGUI fm = new FormGUI();
                        fm.Show();
                    }
                    else
                    {
                        MessageBox.Show("Login Failed!");
    
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void txt_code_TextChanged(object sender, EventArgs e)
            {
                if (txt_code.Text == "0815")
                {
                    FormLogin f1 = (FormLogin)this.Owner;
                    f1.attempt = 3;
                    this.Hide();
                }
            }
        }
    }


    Friday, February 14, 2020 8:21 AM
  • Hi Patrick,

    That is because the attempt variable is static. If you need it to be static, that's fine, but then you'll need to access it like this:

    private void txt_code_TextChanged(object sender, EventArgs e)
    {
        if (txt_code.Text == "0815")
        {
            FormLogin.attempt = 3;
            this.Hide();
        }
    }


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, February 14, 2020 4:19 PM
  • Thank you very much. You helped me. I still have a problem. If I enter the username or password incorrectly 3 times and if I click again on login. Then count the failed attempts. Actually, if I enter the wrong password 3 times and click on login. May then no longer count. What do I have to do?

    My Programm:

    button2 = Login

    namespace Projektarbeit_Pförtneranlage
    {
        public partial class FormLogin : Form
        {
            static int attempt = 3;
            public FormLogin()
            {
                InitializeComponent();
            }
            //Verbindungszeichenfolge
            string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\patri\Downloads\LoginApplicationSourceCode\LoginApplicationSourceCode\LoginApplication\LoginApplication\MyDatabase.mdf;Integrated Security=True;Connect Timeout=30;";
    
            private void button2_Click(object sender, EventArgs e)
            {
                string username = txt_name.Text;
                string password = txt_pwd.Text;
    
                if (txt_name.Text == "" || txt_pwd.Text == "")
                {
                    attempt = 0;
    
                    MessageBox.Show("Bitte geben Sie Benutzername und Passwort ein!");
                    return;
                }
                else if ((attempt == 3) && (attempt > 0))
                {
                    lbl_versuch.Text = ("Sie haben nur noch " + Convert.ToString(attempt) + " Versuche.");
                    --attempt;
                }
                else if ((attempt == 2) && (attempt > 0))
                {
                    lbl_versuch.Text = ("Sie haben nur noch " + Convert.ToString(attempt) + " Versuche.");
                    --attempt;
                }
                else if ((attempt == 1) && (attempt > 0))
                {
                    lbl_versuch.Text = ("Sie haben nur noch " + Convert.ToString(attempt) + " Versuch.");
                    --attempt;
                }
                else if (attempt == 0)
                {
                    FormLogin f2 = new FormLogin();
                    f2.ShowDialog(this);
    
                }
    
                try
                {
                    //Erstellen Sie SqlConnection
                    SqlConnection con = new SqlConnection(cs);
                    SqlCommand cmd = new SqlCommand("Select * from tbl_Login where UserName=@username and Password=@password", con);
                    cmd.Parameters.AddWithValue("@username", txt_name.Text);
                    cmd.Parameters.AddWithValue("@password", txt_pwd.Text);
                    con.Open();
                    SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adapt.Fill(ds);
                    con.Close();
                    int count = ds.Tables[0].Rows.Count;
                    //Wenn count gleich 1 ist, wird die Form frmMain angezeigt
                    if (count == 1)
                    {
                        MessageBox.Show("Anmeldung erfolgreich!");
                        this.Hide();
                        FormGUI fm = new FormGUI();
                        fm.Show();
                    }
                    else
                    {
                        MessageBox.Show("Login Failed!");
    
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void txt_code_TextChanged(object sender, EventArgs e)
            {
                if (txt_code.Text == "0815")
                {
                    FormLogin.attempt = 3;
                    this.Hide();
                }
            }
        }
    }

    Friday, February 14, 2020 6:11 PM
  • Hi Patrick96,
    You can add a TextBox control and  move the following code to your FormLogin.cs.it change the value of attempt to 3.Then you can enter the username and password again.

    private void txt_code_TextChanged(object sender, EventArgs e)
            {
                if (txt_code.Text == "0815")
                {
                    FormLogin.attempt = 3;
                    this.Hide();
                }
            }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 17, 2020 6:02 AM
  • Hi Patrick96,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 4, 2020 8:02 AM