// event method for when the submit button is clicked private void btnNewBook_Click(object sender, EventArgs e) { // converts the date in the dtTime control to text DateTime date = Convert.ToDateTime(dtDate.Text); // if the booking is not already taken if (!db.checkBookingExists(dtDate.Text, Convert.ToInt32(txtPeriod.Value), Convert.ToInt32(txtRoom.Value), editID)) { // if in edit mode if (modeEdit) { // update the booking db.updateBooking(editID, Convert.ToInt32(txtRoom.Value), date, Convert.ToInt32(txtPeriod.Value), session.userID, txtNotes.Text); help.refreshHomeForm(); } else { // insert the booking db.insertBooking(Convert.ToInt32(txtRoom.Value), date, Convert.ToInt32(txtPeriod.Value), session.userID, txtNotes.Text); help.refreshHomeForm(); } // close the form this.Close(); } else { // show error message MessageBox.Show("That slot is taken!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// event method for when the submit button is clicked private void btnLoginEnter_Click(object sender, EventArgs e) { // gets the data from the form string username = txtLoginUsername.Text; string password = txtLoginPassword.Text; // attempts to query the database to check details - returns data loginReturnedData loginData = db.checkLoginDetails(username, password); // if login successful if (loginData.success) { // set session details to user details session.userID = loginData.userID; session.username = loginData.username; session.name = loginData.name; session.role = loginData.role; session.email = loginData.email; session.loggedIn = true; helper.refreshHomeForm(); // close form this.Close(); } else { // sets error message lblLoginError.Text = "Login failed!"; // empties the password field txtLoginPassword.Text = String.Empty; } }
// when the submit button is clicked private void btnSubmit_Click(object sender, EventArgs e) { // default the password given to null string givenPassword = null; // CHECK: password match if (!String.IsNullOrEmpty(txtPass1.Text)) { // if both passwords are tge sane if (txtPass1.Text == txtPass2.Text) { // the given password is that has been entered into txtPass1 givenPassword = txtPass1.Text; } // else throw error message, abandon checks and force password reattempt else { MessageBox.Show("The two passwords you entered don't match!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // CHECK: username already exists if (db.checkUsernameExists(txtUsername.Text, userID)) { // throw error message to user MessageBox.Show("That username is taken!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // CHECK: username alphanumeric Regex alphanumeric = new Regex("^[a-zA-Z0-9]*$"); // if the username does not meet the regex requirements: throw error message if (!alphanumeric.IsMatch(txtUsername.Text)) { MessageBox.Show("The username should only contain alphanumeric characters! (A-Z, a-z and 0-9)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // CHECK: username length is less than or = 15 if (txtUsername.Text.Length > 15) { MessageBox.Show("The username should be 15 characters or less!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // if form is set to new user mode if (newUserMode) { try { // set SQL command to INSERT string cmd = String.Format("INSERT INTO tblUsers (FirstName, SecondName, Password, Role, Username) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}')", txtName1.Text, txtName2.Text, txtPass1.Text, bxRoleList.Text, txtUsername.Text); // send the command to the Database server db.miscAction(cmd); // refresh the home form to accomodate for new data helper.refreshHomeForm(); // close this form this.Close(); } // if error in the sending of the SQL command: throw to user catch (Exception ex) { MessageBox.Show("An unexpected error occured. \nDetails: " + ex.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // if form is in edit mode else { // if no new password is given, set the given password to their current password if (String.IsNullOrEmpty(givenPassword)) { givenPassword = user.password; } try { // call updateuser method and pass through all user details db.updateUser(userID, txtName1.Text, txtName2.Text, givenPassword, bxRoleList.Text, txtUsername.Text); // if the user being edited is the current logged in user if (session.userID == userID) { // change their name shown at top to updated name session.name = new string[] { txtName1.Text, txtName2.Text }; } // refresh the home form to show new details helper.refreshHomeForm(); // close the form this.Close(); } // if an error occurs, throw to user catch (Exception ex) { MessageBox.Show("An unexpected error occured. \nDetails: " + ex.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }