예제 #1
0
        private void SignUpButtonSignUpForm_Click(object sender, EventArgs e)
        {
            // Get connection string from configuration file.
            var connectionString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;

            MySqlConnection connection = new MySqlConnection(connectionString);

            connection.Open();

            // SQL queries.
            string query     = "INSERT INTO Users(Username, Password) VALUES(@username, @password)";
            string userQuery = "SELECT count(*) FROM Users WHERE Username = @usernamecheck";

            MySqlCommand cmd             = new MySqlCommand(query, connection);
            MySqlCommand checkUserExists = new MySqlCommand(userQuery, connection);

            checkUserExists.Parameters.AddWithValue("@usernamecheck", usernameTextBoxSignUpForm.Text);

            int UserExists = Convert.ToInt32(checkUserExists.ExecuteScalar());

            // If the username entered is taken already, display a message to the user.
            if (UserExists > 0)
            {
                MessageBox.Show("Username is already in use!\nPlease choose another", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                usernameTextBoxSignUpForm.Clear();
                passwordTextBoxSignUpForm.Clear();
                confirmPasswordTextboxSignUpForm.Clear();
                usernameTextBoxSignUpForm.Focus();
            }
            else
            {
                // Checks for blank text boxes and display a message to the user.
                if (usernameTextBoxSignUpForm.Text == "" || passwordTextBoxSignUpForm.Text == "" || confirmPasswordTextboxSignUpForm.Text == "")
                {
                    MessageBox.Show("Please fill in all details", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }

                if (passwordTextBoxSignUpForm.Text.Equals(confirmPasswordTextboxSignUpForm.Text))
                {
                    // Add username and password to database.
                    cmd.Parameters.AddWithValue("@username", usernameTextBoxSignUpForm.Text);
                    cmd.Parameters.AddWithValue("@password", passwordTextBoxSignUpForm.Text);

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("Sign up Successful!\n\nHello " + usernameTextBoxSignUpForm.Text + "\n");

                    connection.Close();

                    LoginForm loginForm = new LoginForm();

                    // Close current form and open login form in same position.
                    this.Hide();

                    loginForm.StartPosition = FormStartPosition.Manual;
                    loginForm.Location      = Location;
                    loginForm.Size          = Size;

                    loginForm.Show();
                }
                else
                {
                    // If passwords don't match show message to user.
                    MessageBox.Show("Passwords do not match!");
                    usernameTextBoxSignUpForm.Clear();
                    passwordTextBoxSignUpForm.Clear();
                    confirmPasswordTextboxSignUpForm.Clear();
                }
            }
        }