//Checking if the user exists public Boolean checkUsername() { MY_DB db = new MY_DB(); String username = textBoxUsername.Text; DataTable table = new DataTable(); MySqlDataAdapter adapter = new MySqlDataAdapter(); MySqlCommand command = new MySqlCommand("SELECT * FROM `users` WHERE `username`= @usn", db.getConnection()); command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = username; adapter.SelectCommand = command; adapter.Fill(table); //Checking if this username already exists in the database if (table.Rows.Count > 0) { return(true); } else { return(false); } }
private void registerbtn_Click(object sender, EventArgs e) { MY_DB db = new MY_DB(); MySqlCommand command = new MySqlCommand("INSERT INTO `users`(`First Name`, `Last Name`, `Email`, `Username`, `Password`, `User Type`) VALUES (@fn, @ln, @email, @usn, @pass, @ustype)", db.getConnection()); command.Parameters.Add("@fn", MySqlDbType.VarChar).Value = textBox1.Text; command.Parameters.Add("@ln", MySqlDbType.VarChar).Value = textBox2.Text; command.Parameters.Add("@email", MySqlDbType.Text).Value = textBox3.Text; command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = textBoxUsername.Text; command.Parameters.Add("@pass", MySqlDbType.VarChar).Value = textBoxPassword.Text; command.Parameters.Add("@ustype", MySqlDbType.VarChar).Value = comboBox1.Text; //Opening the cnnection db.openConnection(); if (!checkTextBoxesValues()) { //Checking if password is equal to confirm password if (textBoxPassword.Text.Equals(textBox6.Text)) { if (checkUsername()) { MessageBox.Show("This Username Already Exists. Try a Different One!", "Duplicate Username", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); } else { //Execute the query if (command.ExecuteNonQuery() == 1) { MessageBox.Show("Your Account Has Been Created", "Account", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("ERROR"); } } } else { MessageBox.Show("Please Confirm Password", "Password Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); } } else { MessageBox.Show("Please Enter Your Information", "Empty Data", MessageBoxButtons.OK, MessageBoxIcon.Information); } //Closing the connection db.closeConnection(); }
private void button1_Click(object sender, EventArgs e) { MY_DB db = new MY_DB(); String username = textBoxUsername.Text; String password = textBoxPassword.Text; String userType = comboBox1.Text; string cmbItemValue = comboBox1.SelectedItem.ToString(); MySqlDataAdapter adapter = new MySqlDataAdapter(); DataTable table = new DataTable(); MySqlCommand command = new MySqlCommand("SELECT * FROM `users` WHERE `Username`=@usn AND `Password`=@pass AND `User Type`=@ustype", db.getConnection()); command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = textBoxUsername.Text; command.Parameters.Add("@pass", MySqlDbType.VarChar).Value = textBoxPassword.Text; command.Parameters.Add("@ustype", MySqlDbType.VarChar).Value = comboBox1.Text; //Opening the cnnection db.openConnection(); adapter.SelectCommand = command; adapter.Fill(table); //Checking if the users exists if (table.Rows.Count > 0) { for (int i = 0; i < table.Rows.Count; i++) { if (table.Rows[i]["User Type"].ToString() == cmbItemValue) { MessageBox.Show("You Are Logged in as " + table.Rows[i][2]); if (comboBox1.SelectedIndex == 0) { GLOBAL.username = table.Rows[0]["Username"].ToString(); GLOBAL.userType = table.Rows[0]["User Type"].ToString(); this.Hide(); Dashboard dashboard = new Dashboard(); dashboard.Show(); } else if (comboBox1.SelectedIndex == 1) { GLOBAL.username = table.Rows[0]["Username"].ToString(); GLOBAL.userType = table.Rows[0]["User Type"].ToString(); this.Hide(); Sailfish_Dashboard sailfishDash = new Sailfish_Dashboard(); sailfishDash.Show(); } else if (comboBox1.SelectedIndex == 2) { GLOBAL.username = table.Rows[0]["Username"].ToString(); GLOBAL.userType = table.Rows[0]["User Type"].ToString(); this.Hide(); Grenfin_Dashboard grenfinDash = new Grenfin_Dashboard(); grenfinDash.Show(); } else if (comboBox1.SelectedIndex == 3) { GLOBAL.username = table.Rows[0]["Username"].ToString(); GLOBAL.userType = table.Rows[0]["User Type"].ToString(); this.Hide(); Dolphin_Dashboard dolphinDash = new Dolphin_Dashboard(); dolphinDash.Show(); } else if (comboBox1.SelectedIndex == 4) { GLOBAL.username = table.Rows[0]["Username"].ToString(); GLOBAL.userType = table.Rows[0]["User Type"].ToString(); this.Hide(); Grenada_Team_Dashboard grenTeamDash = new Grenada_Team_Dashboard(); grenTeamDash.Show(); } } } } else { if (username.Trim().Equals("")) { MessageBox.Show("Please Enter Your Username to Login!", "Empty Username", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (userType.Trim().Equals("")) { MessageBox.Show("Please Check The User Type!", "Empty User Type", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (password.Trim().Equals("")) { MessageBox.Show("Please Check Your Password!", "Empty Password", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("Please Check Username OR Password!", "Wrong Data", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }