private void BtnAddOrUpdate_Click(object sender, EventArgs e) { if (!FieldsAreValid()) { return; } if (CboRoles.SelectedIndex < 0) { Role newRole = new Role() { Level = Convert.ToByte(TxtLevel.Text), Name = TxtName.Text, IsTechnician = CbxTechnician.Checked, CanDeleteTickets = CbxDeleteTickets.Checked, CanChangePriority = CbxChangePriority.Checked, CanAssignTickets = CbxAssignTickets.Checked, CanEditRoles = CbxEditRoles.Checked, CanEditUsers = CbxEditUsers.Checked }; try { RoleDB.Add(newRole); MessageBox.Show("Role saved succesfuly"); RefreshForm(); } catch { MessageBox.Show("Error: Role could not be saved"); } } else { Role selRole = (Role)CboRoles.SelectedItem; selRole.Level = Convert.ToByte(TxtLevel.Text); selRole.Name = TxtName.Text; selRole.IsTechnician = CbxTechnician.Checked; selRole.CanDeleteTickets = CbxDeleteTickets.Checked; selRole.CanChangePriority = CbxChangePriority.Checked; selRole.CanAssignTickets = CbxAssignTickets.Checked; selRole.CanEditRoles = CbxEditRoles.Checked; selRole.CanEditUsers = CbxEditUsers.Checked; try { RoleDB.Update(selRole); MessageBox.Show("Role successfuly updated"); RefreshForm(); } catch { MessageBox.Show("Error: Role not updated succesfuly"); } } }
private void BtnDelete_Click(object sender, EventArgs e) { Role selRole = (Role)CboRoles.SelectedItem; DialogResult result = MessageBox.Show($"Are you sure you want to delete {selRole.Name}?", "Confirmation", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { try { RoleDB.Delete(selRole); MessageBox.Show($"{selRole} deleted successfuly"); RefreshForm(); } catch { MessageBox.Show($"Error: {selRole} was not deleted successfuly"); } } }
/// <summary> /// Gets a list of all Roles from the database and databinds to Role comboBox /// </summary> private void PopulateRoleComboBox() { CboRole.DataSource = RoleDB.GetAllRoles(); }
private void PopulateRoleComboBox() { CboRoles.DataSource = RoleDB.GetAllRoles(); CboRoles.DisplayMember = nameof(Role.Name); }