private void addUser() { if ((tb_fname.Text == "" || tb_lname.Text == "" || tb_email.Text == "" || tb_password.Text == "" || comboBox1.Text == "") || (checkBox1.Checked != true && checkBox2.Checked != true && checkBox3.Checked != true)) { MessageBox.Show("Pleae fill all fields.", "Empty Fields"); } else { string connectionString = "datasource = localhost; port = 3306; username = root; password = password; database = FontbonneDay; SslMode=none"; MySqlConnection dbConnect = new MySqlConnection(connectionString); try { dbConnect.Open(); } catch (Exception ex) { MessageBox.Show("The connection failed!", "Connection Failed"); DumpException(ex); } string stat; if (checkBox1.Checked == true) { stat = checkBox1.Text; } else if (checkBox2.Checked == true) { stat = checkBox2.Text; } else { stat = checkBox3.Text; } string query = "Insert into users (firstName, lastName, email, password, status, departmentName) values (@fName, @lName, @Email, @Password, @Status, @Department)"; MySqlCommand command = new MySqlCommand(query, dbConnect); command.Parameters.AddWithValue("@fName", tb_fname.Text); command.Parameters.AddWithValue("@lName", tb_lname.Text); command.Parameters.AddWithValue("@Email", tb_email.Text); command.Parameters.AddWithValue("@Password", tb_password.Text); command.Parameters.AddWithValue("@Status", stat); command.Parameters.AddWithValue("Department", comboBox1.Text); command.ExecuteNonQuery(); dbConnect.Close(); this.userId = findUserID(); ViewEvents viewEvents = new ViewEvents(userId); viewEvents.Show(); } }
private void Login() { string connectionString = "datasource = localhost; port = 3306; username = root; password = password; database = FontbonneDay; SslMode=none"; MySqlConnection dbConnect = new MySqlConnection(connectionString); try { dbConnect.Open(); } catch (Exception ex) { MessageBox.Show("The connection failed!", "Connection Failed"); DumpException(ex); } string query = "SELECT COUNT(*) FROM users WHERE email = @Email AND password = @Pass"; MySqlCommand command = new MySqlCommand(query, dbConnect); command.Parameters.AddWithValue("@Email", tb_email.Text); command.Parameters.AddWithValue("@Pass", tb_pass.Text); int userExist = Convert.ToInt32(command.ExecuteScalar()); if (userExist > 0) { //username exists if (tb_email.Text != "" && tb_pass.Text != "") { this.userId = findUserID(); ViewEvents viewEvents = new ViewEvents(userId); viewEvents.Show(); } else { MessageBox.Show("Please enter both email and password.", "Empty Fields"); } } else { //username doesn't exist MessageBox.Show("Username or password is invalid", "Invalid Account"); } dbConnect.Close(); }