private void AddUserForm_Load(object sender, EventArgs e) { session1Entities db = new session1Entities(); db.Offices.Load(); this.officeBindingSource.DataSource = db.Offices.Local.ToBindingList(); }
private void tbn_Login_Click(object sender, EventArgs e) { if (attempt == 0) { this.btn_Login.Enabled = false; aTimer.Start(); } using (session1Entities db = new session1Entities()) { string username = this.txt_username.Text; string password = this.txt_password.Text; User user = db.Users.FirstOrDefault(p => p.Email == username && p.Password == password); if (user == null) { this.lbl_Login_Result.Text = "Invalid user name or password."; this.txt_username.Focus(); attempt--; } else if (user.RoleID == 1) { this.Hide(); AdministratorForm adminForm = new AdministratorForm(); adminForm.ShowDialog(); } else if (user.RoleID == 2) { this.Hide(); UserForm userForm = new UserForm(); userForm.ShowDialog(); } }; }
private void btn_Add_User_Click(object sender, EventArgs e) { session1Entities db = new session1Entities(); User newUser = new User(); newUser.ID = db.Users.Max(u => u.ID) + 1; newUser.Email = this.txt_email.Text; User existedEmail = db.Users.FirstOrDefault(p => p.Email == this.txt_email.Text); if (existedEmail != null) { MessageBox.Show("Existed Email Address"); return; } newUser.FirstName = this.txt_first_name.Text; newUser.LastName = this.txt_last_name.Text; if (this.pickerBirthDate.Text != "") { newUser.Birthdate = DateTime.Parse(pickerBirthDate.Text); } newUser.Password = this.txt_password.Text; newUser.Active = true; newUser.RoleID = 2;//default User Role =2 db.Users.Add(newUser); try { db.SaveChanges(); MessageBox.Show("Create user successfully"); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); MessageBox.Show(fullErrorMessage); } }