/// <summary> /// Set the default DataGridView in the "review" tab /// </summary> /// <param name="mysql_cmd">The selection command</param> public void SetDefaultSearch(string mysql_cmd) { if (db_connect.OpenConnection()) { if (view_search_option == "history") { mysql_cmd += "ORDER BY history_info.id_inventory ASC"; } ds_search = new DataSet(); MySqlCommand cmd = new MySqlCommand(mysql_cmd, db_connect.Connection); MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd); dataAdapter.Fill(ds_search); dataGridView_view.DataSource = ds_search.Tables[0].DefaultView; } else { MessageBox.Show(db_connect.Message); return; } if (!db_connect.CloseConnection()) { MessageBox.Show(db_connect.Message); return; } }
/// <summary> /// When the register button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_register_Click(object sender, EventArgs e) { username = textBox_username.Text; password = textBox_password.Text; email = textBox_email.Text; // Check username input if (!checkTextAsCharater(username)) { MessageBox.Show("Invalid username!"); return; } // Check password input if (!checkTextAsCharater(password)) { MessageBox.Show("Invalid password!"); return; } // Check email input if (!checkTextAsEmail(email)) { MessageBox.Show("Invalid email!"); return; } // Register as personal client if (usertype == "personal") { firstname = textBox_firstname.Text; middlename = textBox_middlename.Text; lastname = textBox_lastname.Text; if (!checkTextAsCharater(firstname)) { MessageBox.Show("Invalid first name!"); return; } if (middlename.Length != 0) { if (!checkTextAsCharater(middlename)) { MessageBox.Show("Invalid middle name!"); return; } } if (!checkTextAsCharater(lastname)) { MessageBox.Show("Invalid last name!"); return; } } // Register as corporate client else { companyname = textBox_companyname.Text; if (!checkTextAsCharater(companyname)) { MessageBox.Show("Invalid company name!"); return; } } if (db_connect.OpenConnection()) { string mysql_cmd; MySqlCommand cmd; // Insert the new client if (usertype == "personal") { mysql_cmd = "INSERT INTO `gowaredb`.`client` (`usertype`, `username`, `password`, `email`, `firstname`, `middlename`, `lastname`, `sex`) " + "VALUES ('personal', @username, @password, @email, @firstname, @middlename, @lastname, @sex);"; cmd = new MySqlCommand(mysql_cmd, db_connect.Connection); cmd.Parameters.AddWithValue("@firstname", firstname); cmd.Parameters.AddWithValue("@middlename", middlename); cmd.Parameters.AddWithValue("@lastname", lastname); cmd.Parameters.AddWithValue("@sex", sex); } else { mysql_cmd = "INSERT INTO `gowaredb`.`client` (`usertype`, `username`, `password`, `email`, `companyname`) " + "VALUES ('corporate', @username, @password, @email, @companyname);"; cmd = new MySqlCommand(mysql_cmd, db_connect.Connection); cmd.Parameters.AddWithValue("@companyname", companyname); } cmd.Parameters.AddWithValue("@username", username); cmd.Parameters.AddWithValue("@password", password); cmd.Parameters.AddWithValue("@email", email); if (cmd.ExecuteNonQuery() > 0) { MessageBox.Show("Registered Successfully!"); this.Close(); } } else { MessageBox.Show(db_connect.Message); } if (!db_connect.CloseConnection()) { MessageBox.Show(db_connect.Message); } }
/// <summary> /// Get a specific category fee by ID /// </summary> /// <param name="categoryID"></param> /// <returns></returns> private Dictionary <string, string> GetFeeCategory(string categoryID) { Dictionary <string, string> dict = new Dictionary <string, string>(); dict.Add("fee", "0"); if (db_connect.OpenConnection()) { string mysql_cmd = "SELECT * FROM category WHERE id_category = @categoryID"; MySqlCommand cmd = new MySqlCommand(mysql_cmd, db_connect.Connection); cmd.Parameters.AddWithValue("@categoryID", categoryID); MySqlDataReader dataReader = cmd.ExecuteReader(); while (dataReader.Read()) { dict["fee"] = dataReader["fee"].ToString(); } } else { MessageBox.Show(db_connect.Message); } if (!db_connect.CloseConnection()) { MessageBox.Show(db_connect.Message); } return(dict); }
/// <summary> /// When the register button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_register_Click(object sender, EventArgs e) { username = textBox_username.Text; password = textBox_password.Text; email = textBox_email.Text; firstname = textBox_firstname.Text; middlename = textBox_middlename.Text; lastname = textBox_lastname.Text; key = textBox_key.Text; string keyID = ""; // Check username if (!checkTextAsCharater(username)) { MessageBox.Show("Invalid username!"); return; } // Check password if (!checkTextAsCharater(password)) { MessageBox.Show("Invalid password!"); return; } // Check email if (!checkTextAsEmail(email)) { MessageBox.Show("Invalid email!"); return; } // Check name if (!checkTextAsCharater(firstname)) { MessageBox.Show("Invalid first name!"); return; } if (middlename.Length != 0) { if (!checkTextAsCharater(middlename)) { MessageBox.Show("Invalid middle name!"); return; } } if (!checkTextAsCharater(lastname)) { MessageBox.Show("Invalid last name!"); return; } // Check whether the manage key is valid keyID = judgeKey(key); if (key.Length == 0 || keyID == "") { MessageBox.Show("Invalid Management Key!"); return; } if (db_connect.OpenConnection()) { string mysql_cmd = "INSERT INTO `gowaredb`.`manager` (`id_key`, `username`, `password`, `email`, `firstname`, `middlename`, `lastname`, `sex`) " + "VALUES (@keyID, @username, @password, @email, @firstname, @middlename, @lastname, @sex);"; MySqlCommand cmd = new MySqlCommand(mysql_cmd, db_connect.Connection); cmd.Parameters.AddWithValue("@keyID", keyID); cmd.Parameters.AddWithValue("@username", username); cmd.Parameters.AddWithValue("@password", password); cmd.Parameters.AddWithValue("@email", email); cmd.Parameters.AddWithValue("@firstname", firstname); cmd.Parameters.AddWithValue("@middlename", middlename); cmd.Parameters.AddWithValue("@lastname", lastname); cmd.Parameters.AddWithValue("@sex", sex); if (cmd.ExecuteNonQuery() > 0) { MessageBox.Show("Registered Successfully!"); this.Close(); } else { MessageBox.Show(db_connect.Message); } if (!db_connect.CloseConnection()) { MessageBox.Show(db_connect.Message); } } }
/// <summary> /// When the client log in button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_client_login_Click(object sender, EventArgs e) { client.Username = textBox_username_client.Text; client.Password = textBox_pwd_client.Text; bool logInSucc = false; // Check the username if (client.Username == "") { MessageBox.Show("Please enter the Username."); logInSucc = false; } else if (!checkText(client.Username)) { MessageBox.Show("Invalid Username."); logInSucc = false; } // Check the password else if (client.Password == "") { MessageBox.Show("Please enter the Password."); logInSucc = false; } else { if (db_connect.OpenConnection()) { string mysql_cmd = "SELECT * FROM client " + "WHERE username = @name " + "AND password = @pwd;"; MySqlCommand cmd = new MySqlCommand(mysql_cmd, db_connect.Connection); cmd.Parameters.AddWithValue("@name", client.Username); cmd.Parameters.AddWithValue("@pwd", client.Password); MySqlDataReader dataReader = cmd.ExecuteReader(); // If the client can be found in the database, log in succeed while (dataReader.Read()) { if (dataReader["username"].ToString() == client.Username && dataReader["password"].ToString() == client.Password) { client.ID = dataReader["id_client"].ToString(); client.Type = dataReader["usertype"].ToString(); client.Email = dataReader["email"].ToString(); if (client.Type == "personal") { client.Firstname = dataReader["firstname"].ToString(); client.Middlename = dataReader["middlename"].ToString(); client.Lastname = dataReader["lastname"].ToString(); client.Sex = dataReader["sex"].ToString(); } else if (client.Type == "corporate") { client.Companyname = dataReader["companyname"].ToString(); } else { MessageBox.Show("Client type error!"); this.Close(); } logInSucc = true; break; } else { MessageBox.Show("DB Error!"); } } if (!logInSucc) { MessageBox.Show("The Username or the Password is incorrect"); textBox_pwd_client.Clear(); } } else { logInSucc = false; MessageBox.Show(db_connect.Message); } } // If login succeed, switch to the client main form if (logInSucc) { switchToClientMainForm(); } if (!db_connect.CloseConnection()) { MessageBox.Show(db_connect.Message); } }