0

I am coding an Application with my friend and I am having some trouble figuring out how to make the 3rd textbox contain a certain word/key to continue in order to make the account

Here is what the form looks like

enter image description here

Then here is the entire code for the database part of where it creates the account.

private void button5_Click(object sender, EventArgs e)
        {
            if (!textBox1.Text.Equals("") && !textBox2.Text.Equals("") && textBox2.Text.Equals(textBox3.Text))
            {
                StringBuilder sb = new StringBuilder();
                using (SHA256 hash = SHA256Managed.Create())
                {
                    Encoding enc = Encoding.UTF8;
                    Byte[] result = hash.ComputeHash(enc.GetBytes(textBox2.Text));

                    foreach (Byte b in result)
                    {
                        sb.Append(b.ToString("x2"));
                    }
                }

                string connectionString = "datasource=127.0.0.1;port=3306;username=root;password=;database=majorpayne;";

                string query = "INSERT INTO staff(USERNAME, PASSWORD) VALUES('" + textBox1.Text + "', '" + sb.ToString() + "')";
                string query2 = "SELECT * FROM staff WHERE username='" + textBox1.Text + "' AND password='" + sb.ToString() + "'";

                MySqlConnection con = new MySqlConnection(connectionString);
                MySqlConnection databaseConnection = new MySqlConnection(connectionString);
                MySqlCommand insertCommand = new MySqlCommand(query, databaseConnection);
                MySqlCommand checkCommand = new MySqlCommand(query2, databaseConnection);
                MySqlDataReader reader;

                try
                {
                    databaseConnection.Open();
                    reader = insertCommand.ExecuteReader();
                    reader.Close();
                    reader = checkCommand.ExecuteReader();

                    if (reader.HasRows)
                    {
                        MessageBox.Show("Successfully Created Account.");
                        {
                            Login main = new Login();
                            main.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Database Error (404)");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}

If anyone can help make a way where the textBox3 equals a specific word thanks in advance.

And for a short explanation, I want the textBox3 to have a "key" in it that checks if the key is the exact key and if the correct key is there, it goes on and creates the account with the user/pass that was entered.

Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21
  • what C# **type** is the **key** you wanted, and give a sample value? – Lei Yang Jun 30 '21 at 02:03
  • where you have used textbox3 in the code ? – Amit Verma Jun 30 '21 at 02:13
  • @LeiYang so what the key is doesn't really matter it could be a word or an actual key but what I am going for is something like where I put the "key" in the textbox3 and if it is correct it continues and creates the account with the username and password, but if the key is wrong it makes a popup and says the key is invalid. – NotRealWolf Jun 30 '21 at 03:46
  • why don't you call it string, but call it key? – Lei Yang Jun 30 '21 at 03:47
  • I'm just trying to have it look like this Username: NotRealWolf Password: ************ Key: AE181FDDF9B1C And then if the key is valid then it will pop up with a box saying "Account Created!" Then in the database, the account is created under staff. – NotRealWolf Jun 30 '21 at 04:07
  • You need to fix the mistakes [we've pointed out previously](https://stackoverflow.com/questions/68171278/). That's [not how to hash passwords](https://security.stackexchange.com/questions/130997/). That's [not how to construct SQL commands](https://stackoverflow.com/questions/7505808/). That's [not how to run a SQL INSERT](https://stackoverflow.com/questions/2974154/). – Dour High Arch Jun 30 '21 at 15:34

1 Answers1

0

you can add label Under textBox3 Then on textBox3_KeyUp Event Write your Code Like This

private void textBox3_KeyUp(object sender, KeyEventArgs e)
    {
        if (textBox1.Text == "Your Key")
        {
            label1.Text = "correct key";
            label1.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            label1.Text = "wrong key";
            label1.ForeColor = System.Drawing.Color.Red;
        }
    }
  • Thanks, I like that Idea but I am also trying to make it secure so it checks if that is the key and if it's not it won't make the account in the database. – NotRealWolf Jun 30 '21 at 05:00