private void save_Click(object sender, EventArgs e) { int admin = 0; try { if (String.IsNullOrEmpty(textBoxUsername.Text) || String.IsNullOrEmpty(textBoxPass.Text) || String.IsNullOrEmpty(textBoxVeryfiPass.Text) || String.IsNullOrEmpty(textBoxName.Text) || String.IsNullOrEmpty(textBoxSurname.Text)) { throw new Exception("Wszystkie dane muszą być uzupełnione."); } if (comboBoxAdmin.SelectedIndex == 1) { admin = 1; } if (String.Compare(textBoxPass.Text, textBoxVeryfiPass.Text) == -1) { throw new Exception("Wpisane hasła muszą być identycze."); } foreach (var user in context.Users) { if (user.Username == textBoxUsername.Text) { throw new Exception("Wskazany użytkownik już istnieje w bazie danych.\nProszę zmień nazwę użytkownika."); } } context.Users.Add(new Users { Username = textBoxUsername.Text, Password = CreateMD5(textBoxPass.Text), Name = textBoxName.Text, Surname = textBoxSurname.Text, Admin = admin }); context.SaveChanges(); MessageBox.Show("Użytkownik został pomyślnie dodany."); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void deleteUser_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Czy jesteś pewien że chcesz usunąć użytkownika?", "Uwaga", MessageBoxButtons.YesNo) == DialogResult.Yes) { int rows = dataGridView.RowCount - 1; for (int i = rows; i >= 0; i--) { if (dataGridView.Rows[i].Selected) { context.Users.Remove(dataGridView.Rows[i].DataBoundItem as Users); context.SaveChanges(); } } dataGridView.DataSource = context.Users.ToList(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void save_Click(object sender, EventArgs e) { try { if (String.IsNullOrEmpty(textBoxUsername.Text) || String.IsNullOrEmpty(textBoxName.Text) || String.IsNullOrEmpty(textBoxSurname.Text)) { throw new Exception("Wszystkie dane muszą być uzupełnione."); } if (comboBoxAdmin.SelectedIndex == 1) { admin = 1; } var result = context.Users.SingleOrDefault(b => b.Username == textBoxUsername.Text); if (result != null) { result.Name = textBoxName.Text; result.Surname = textBoxSurname.Text; result.Admin = admin; if (textBoxPassword.Text.Length != 0) { result.Password = CreateMD5(textBoxPassword.Text); } context.SaveChanges(); } MessageBox.Show("Użytkownik został pomyślnie zmodyfikowany."); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void buttonNewPass_Click(object sender, EventArgs e) { try { MagazynEntities context = new MagazynEntities(); if (String.IsNullOrEmpty(textBoxPassword.Text) || String.IsNullOrEmpty(textBoxNewPassword.Text) || String.IsNullOrEmpty((textBoxNewPasswordVeryfi.Text))) { throw new Exception("Wszystkie dane muszą być uzupełnione."); } if (String.Compare(textBoxNewPassword.Text, textBoxNewPasswordVeryfi.Text) == -1) { throw new Exception("Wpisane hasła muszą być identycze."); } var result = context.Users.SingleOrDefault(b => b.Username == tmp.Username); if (result != null && result.Password == CreateMD5(textBoxPassword.Text)) { result.Password = CreateMD5(textBoxNewPassword.Text); context.SaveChanges(); } else { throw new Exception("Aktualne hasło jest nieprawidłowe."); } MessageBox.Show("Hasło zostało pomyślnie zmienione."); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }