/// <summary> /// Handles DataGridView button click events /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvUser_CellContentClick(object sender, DataGridViewCellEventArgs e) { var Column = ((DataGridView)sender).Columns[e.ColumnIndex]; if (Column is DataGridViewButtonColumn && e.RowIndex >= 0) { if (Column.Index == 6) // Update button { if (DbConnector.OpenSQLConnection()) // Open connection to the database { // Connection opened UserAdminDataAccess user = new UserAdminDataAccess(); var result = user.FindUser(dgvUser.Rows[e.RowIndex].Cells[0].Value.ToString()); if (result != null) { Users_Tab_Child.getInstance().BringToFront(); Users_Tab_Child.getInstance().changeView(result); } } else { // Connection could not be opened MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } DbConnector.CloseSQLConnection(); // Close connection to the database } else if (Column.Index == 7) // Delete button { if (MessageBox.Show("Are you sure you want to delete this record?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { if (DbConnector.OpenSQLConnection()) // Open connection to the database { // Connection opened UserAdminDataAccess user = new UserAdminDataAccess(); if (user.DeleteUser(dgvUser.Rows[e.RowIndex].Cells[0].Value.ToString())) { // Record deleted successfully MessageBox.Show("Record has been deleted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); populateDataGridView(); } else { // Record was not deleted MessageBox.Show("The record could not be deleted", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { // Connection could not be opened MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } DbConnector.CloseSQLConnection(); // Close connection to the database } } } }
/// <summary> /// Populate the datagridview with search result /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSearchUsers_Click(object sender, EventArgs e) { if (DbConnector.OpenSQLConnection()) // Open connection to the database { // Connection opened UserAdminDataAccess user = new UserAdminDataAccess(); dgvUser.AutoGenerateColumns = false; // To only show the columns needed dgvUser.DataSource = user.SearchUsers(txtSearchUsers.Text.Trim()); } else { // Connection could not be opened MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } DbConnector.CloseSQLConnection(); // Close connection to the database }
/// <summary> /// Method for populating the DataDridView /// </summary> public void populateDataGridView() { if (DbConnector.OpenSQLConnection()) // Open connection to the database { // Connection opened UserAdminDataAccess user = new UserAdminDataAccess(); dgvUser.AutoGenerateColumns = false; // To only show the columns needed dgvUser.DataSource = user.GetAllUsers(); txtSearchUsers.Text = ""; Users_Tab_Child.getInstance().clearUserText(); } else { // Connection could not be opened MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } DbConnector.CloseSQLConnection(); // Close connection to the database }
/// <summary> /// Changing the view for insert or update /// </summary> /// <param name="viewType"></param> public void changeView(Project project) { List <User> users = new List <User>(); if (DbConnector.OpenSQLConnection()) // Open connection to the database { // Connection opened UserAdminDataAccess user = new UserAdminDataAccess(); users = user.GetActiveUsers(); } else { // Connection could not be opened MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (users != null) { // Populate comboBoxes with data foreach (User u in users) { if (u.UserType == "Manager") { comboBoxManager.Items.Add(u.Username); } else if (u.UserType == "Tester") { comboBoxTester.Items.Add(u.Username); } else { comboBoxFrontEndDev.Items.Add(u.Username); comboBoxBackEndDev.Items.Add(u.Username); } } } if (project == null) { // Button Add New clicked labelTitle.Text = "Add New Project"; btnProjectSave.Text = "Save"; txtProjectName.Enabled = true; } else { // Button Update clicked labelTitle.Text = "Update Project"; btnProjectSave.Text = "Update"; projectId = project.ProjectId; txtProjectName.Text = project.ProjectName; txtProjectName.Enabled = false; comboBoxManager.Text = project.Manager; comboBoxFrontEndDev.Text = project.FrontEndDev; comboBoxBackEndDev.Text = project.BackEndDev; comboBoxTester.Text = project.Tester; if (project.Priority == "Low") { RadioButtonPriorityLow.Checked = true; } else if (project.Priority == "Medium") { RadioButtonPriorityMed.Checked = true; } else { RadioButtonPriorityHigh.Checked = true; } dateTimePickerStart.Value = project.StartDate; dateTimePickerEnd.Value = project.EndDate; } }
/// <summary> /// Handle the function of adding an user to the database /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUserSave_Click(object sender, EventArgs e) { string username = txtUsername.Text.Trim(); string userType = comboBoxUserType.Text; string fname = txtFName.Text.Trim(); string lname = txtLName.Text.Trim(); if (username != "" && userType != "" && fname != "" && lname != "" && txtPass.Text != "") // Check if required fields are filled { if (txtPass.Text == txtConfirmPass.Text) // Check whether the password and confirm password matches { if (txtPass.Text.Length >= 8) // Check whether the password length is at least 8 characters { if (DbConnector.OpenSQLConnection()) // Open connection to the database { // Connection opened UserAdminDataAccess user = new UserAdminDataAccess(); if (btnUserSave.Text == "Save") { if (user.IsUsernameExist(username)) // Check if the username already taken { // Username exists MessageBox.Show("Username entered already exists in the database", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (user.InsertUser(username, userType, fname, lname, txtPass.Text, switchUserStatus.IsOn ? "Available" : "Assigned", switchAccStatus.IsOn ? "Active" : "Inactive")) { // Record inserted successfully MessageBox.Show("Record has been inserted successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); Users_Tab.getInstance().BringToFront(); Users_Tab.getInstance().populateDataGridView(); } else { // Record was not inserted MessageBox.Show("The record could not be saved", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (btnUserSave.Text == "Update") { if (user.UpdateUser(username, userType, fname, lname, txtPass.Text, switchUserStatus.IsOn ? "Available" : "Assigned", switchAccStatus.IsOn ? "Active" : "Inactive")) { // Record updated successfully MessageBox.Show("Record has been updated successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); Users_Tab.getInstance().BringToFront(); Users_Tab.getInstance().populateDataGridView(); } else { // Record was not updated MessageBox.Show("The record could not be updated", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { // Connection could not be opened MessageBox.Show("Connection to the database could not be established", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { // Password length less than 8 characters MessageBox.Show("Password should at least contain 8 characters", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { // Password and Confirm Password do not match MessageBox.Show("Password and Confirm Password do not match", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { // Fields not filled correctly MessageBox.Show("Please fill all the required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }