private void btnSave_Click(object sender, EventArgs e) { //Save things to DB and give a success message! var hashedPassword = Hash.sha256(txtPassword.Text); try { using (GlossaryAdminModel dbConnection = new GlossaryAdminModel()) { var newUser = new User(); newUser.Username = txtUsername.Text.ToString(); newUser.Password = hashedPassword; newUser.RoleId = cmbRole.SelectedIndex; newUser.Firstname = txtFirstname.Text.ToString(); newUser.Lastname = txtLastname.Text.ToString(); newUser.Email = txtEmail.Text.ToString(); dbConnection.User.Add(newUser); dbConnection.SaveChanges(); } } catch (EntityException ex) { MessageBox.Show("Could not populate the dropdown list." + ex.Message); } catch (Exception ex) { MessageBox.Show("General error when populating the dropdown list." + ex.Message + ex.InnerException); } finally { Close(); } }
private void btnDelete_Click(object sender, EventArgs e) { using (GlossaryAdminModel dbConnection = new GlossaryAdminModel()) { var selectedUser = (from u in dbConnection.User where u.Id == UserId select u).FirstOrDefault(); if (selectedUser != null) { dbConnection.User.Remove(selectedUser); dbConnection.SaveChanges(); } } panel1.Visible = false; }
public void btnSave_Click(object sender, EventArgs e) { try { using (GlossaryAdminModel dbConnection = new GlossaryAdminModel()) { var selectedUser = (from u in dbConnection.User where u.Id == UserId select u).FirstOrDefault(); if (selectedUser != null) { selectedUser.Username = txtUsername.Text; selectedUser.Firstname = txtFirstname.Text; selectedUser.Lastname = txtLastname.Text; selectedUser.Email = txtEmail.Text; selectedUser.RoleId = cbRole.SelectedIndex; dbConnection.SaveChanges(); } } } catch (EntityException ex) { MessageBox.Show("Could not save the user to database." + ex.Message); } catch (Exception ex) { MessageBox.Show("Error when saving to Database." + ex.Message + ex.InnerException); } finally { showFirstPage(); } }