Exemplo n.º 1
0
        //Opens Login_Screen
        private void Logout_Click(object sender, EventArgs e)
        {
            this.Hide();
            Login_Screen login = new Login_Screen();

            login.ShowDialog();
            login.Focus();
        }
        //********************Non Event Methods********************

        //Creates New user
        private void Register_Click(object sender, EventArgs e)
        {
            //Error message for not filling in mandatory fields
            if (string.IsNullOrWhiteSpace(Input_1.Text) == true || string.IsNullOrWhiteSpace(Input_2.Text) == true ||
                string.IsNullOrWhiteSpace(Input_3.Text) == true || string.IsNullOrWhiteSpace(Input_4.Text) == true ||
                string.IsNullOrWhiteSpace(Input_5.Text) == true || string.IsNullOrWhiteSpace(Input_6.Text) == true ||
                string.IsNullOrWhiteSpace(Input_7.Text) == true)
            {
                MessageBox.Show("All fields need to be filled in", "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Error message if password doesn't match re enter password
            if (Input_4.Text != Input_5.Text)
            {
                MessageBox.Show("Password and re-enter password don't match", "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Error messages if the username is the same or if the first and last name are the same
            string query = "SELECT USER_First_Name, USER_Last_Name, USER_Username FROM [USERS];";

            using (Global.connection = new SQLiteConnection(Global.connectionString))
                using (SQLiteCommand cmd = new SQLiteCommand(query, Global.connection))
                {
                    Global.connection.Open();
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    try
                    {
                        while (reader.Read())
                        {
                            string firstName = Convert.ToString(reader["USER_First_Name"]);
                            string lastName  = Convert.ToString(reader["USER_Last_Name"]);
                            string userName  = Convert.ToString(reader["USER_Username"]);

                            if (Input_3.Text == userName)
                            {
                                MessageBox.Show("This username already exists.  Please choose a different username", "Error",
                                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            //Users may have the same name but is unlikely.  Gives user the option to continue
                            if (Input_1.Text == firstName && Input_2.Text == lastName)
                            {
                                DialogResult areYouSure = MessageBox.Show("There is already a user with the same name:\n" +
                                                                          "UserName: "******"\nName: " + firstName + " " + lastName + "\n\n" +
                                                                          "Are you sure you would like to continue?", "Are you sure", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                                if (areYouSure == DialogResult.No)
                                {
                                    return; //cancels the event action
                                }
                            }
                        }
                        Global.connection.Close();
                    }
                    catch (SQLiteException ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }

            //If no error message is prompted user is created
            UserDatabase.AddUser(Input_1.Text, Input_2.Text, Input_3.Text, Input_4.Text, Input_6.Text, Input_7.Text);
            MessageBox.Show("User successully created\n\nUsername: "******"\nPassword: "******"Register successful", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //returns to login screen
            this.Hide();
            Login_Screen login = new Login_Screen();

            login.ShowDialog();
            login.Focus();
        }