private void EditRoleForm_Load(object sender, EventArgs e) { using (var session = new Session1Entities()) { var query = session.Offices.Select(o => new { o.ID, o.Title }); var result = query.ToList(); comboBoxOffice.DisplayMember = "Title"; comboBoxOffice.ValueMember = "ID"; comboBoxOffice.DataSource = result; } adminMain = (AdminMainForm)Owner; var row = adminMain.dataGridView.CurrentCell.OwningRow; textBoxEmailAddress.Text = row.Cells["EmailAddress"].Value.ToString(); textBoxFirstName.Text = row.Cells["Name"].Value.ToString(); textBoxLastName.Text = row.Cells["LastName"].Value.ToString(); comboBoxOffice.Text = row.Cells["Office"].Value.ToString(); if (row.Cells["UserRole"].Value.ToString() == "Administrator") { radioButtonAdministrator.Checked = true; } else { radioButtonUser.Checked = true; } }
private void button1_Click(object sender, EventArgs e) { string failText; if (radioButtonSoftwareCrash.Checked) { failText = $"Software Crash : {textBoxReason.Text}"; } else { failText = $"System Crash : {textBoxReason.Text}"; } using (var session = new Session1Entities()) { var query = session.Activities.Where(a => a.UserID == id && a.FailReason == "CRASH").FirstOrDefault(); query.FailReason = failText; try { session.SaveChanges(); Close(); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); var fullErrorMessage = string.Join("\n", errorMessages); var exceptionMessage = $"Error : {fullErrorMessage}"; MessageBox.Show(exceptionMessage); } } }
private void buttonApply_Click(object sender, EventArgs e) { if (CheckFields.CheckIfFieldsIsNotEmpty(this)) { var row = adminMain.dataGridView.CurrentCell.OwningRow; // rowEmail is here so that there's no error about LINQ // not being able to convert it to LINQ expression // please don't remove it if (CheckFields.IsValidEmail(textBoxEmailAddress.Text)) { using (var session = new Session1Entities()) { var rowEmail = row.Cells["EmailAddress"].Value.ToString(); var rowSearch = session.Users.Where(u => u.Email == textBoxEmailAddress.Text).FirstOrDefault(); if (rowSearch == null || rowEmail == textBoxEmailAddress.Text) { // id is here so that there's no error about LINQ // not being able to convert it to LINQ expression // please don't remove it var id = int.Parse(row.Cells["ID"].Value.ToString()); var rowEdit = session.Users.Where(u => u.ID == id).FirstOrDefault(); rowEdit.Email = textBoxEmailAddress.Text; rowEdit.FirstName = textBoxFirstName.Text; rowEdit.LastName = textBoxLastName.Text; rowEdit.OfficeID = int.Parse(comboBoxOffice.SelectedValue.ToString()); rowEdit.RoleID = radioButtonAdministrator.Checked ? 1 : 2; try { session.SaveChanges(); adminMain.SetDatagridView(); Close(); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); var fullErrorMessage = string.Join("\n", errorMessages); var exceptionMessage = $"Error : {fullErrorMessage}"; MessageBox.Show(exceptionMessage); } } else { MessageBox.Show("Email Address has already been taken."); } } } else { MessageBox.Show("Please enter a valid email."); } } else { MessageBox.Show("Please don't leave any fields empty."); } }
private void buttonSave_Click(object sender, EventArgs e) { if (CheckFields.CheckIfFieldsIsNotEmpty(this)) { var email = textBoxEmail.Text; if (CheckFields.IsValidEmail(email)) { using (var session = new Session1Entities()) { if (session.Users.Where(u => u.Email == email).FirstOrDefault() == null) { var newUser = new User() { ID = Utilities.GetLastUserID() + 1, Email = textBoxEmail.Text, FirstName = textBoxFirstName.Text, LastName = textBoxLastName.Text, OfficeID = int.Parse(comboBoxOffice.SelectedValue.ToString()), Birthdate = dateTimePickerBirthdate.Value, Password = Hash.MakeMd5(textBoxPassword.Text), RoleID = 2, Active = true }; session.Users.Add(newUser); try { session.SaveChanges(); adminMainForm.SetDatagridView(); ClearFields.ClearTextBoxes(this); dateTimePickerBirthdate.ResetText(); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); var fullErrorMessage = string.Join("\n", errorMessages); var exceptionMessage = $"Error : {fullErrorMessage}"; MessageBox.Show(exceptionMessage); } } else { MessageBox.Show("Please enter another email :\nThat email is already used by another user"); } } } else { MessageBox.Show("Please Enter a valid email."); } } else { MessageBox.Show("Please fill all of the fields first."); } }
private void AddUserForm_Load(object sender, EventArgs e) { adminMainForm = (AdminMainForm)Owner; using (var session = new Session1Entities()) { var query = session.Offices.Select(o => new { o.ID, o.Title }); var result = query.ToList(); comboBoxOffice.DisplayMember = "Title"; comboBoxOffice.ValueMember = "ID"; comboBoxOffice.DataSource = result; } }
private void MonitorForm_Load(object sender, EventArgs e) { using (var session = new Session1Entities()) { var query = session.Activities.Where(a => a.UserID == id && a.FailReason == "CRASH").FirstOrDefault(); if (query != null) { labelLastLogin.Text = labelLastLogin.Text.Replace("{lastDate}", query.Date.ToString()); labelLastLogin.Text = labelLastLogin.Text.Replace("{lastTime}", query.LoginTime.ToString()); } else { Close(); } } }
public void SetDatagridView() { // If value == 0 : All Office will be shown. int value = int.Parse(comboBoxOffice.SelectedValue.ToString()); using (var session = new Session1Entities()) { if (value != 0) { var query = session.Users.Where(u => u.Office.ID == value).Select(u => new { u.ID, Name = u.FirstName, u.LastName, Age = DateTime.Now.Year - u.Birthdate.Value.Year, UserRole = u.Role.Title, EmailAddress = u.Email, Office = u.Office.Title, u.Active }); dataGridView.DataSource = query.ToList(); } else { var query = session.Users.Select(u => new { u.ID, Name = u.FirstName, u.LastName, Age = DateTime.Now.Year - u.Birthdate.Value.Year, UserRole = u.Role.Title, EmailAddress = u.Email, Office = u.Office.Title, u.Active }); dataGridView.DataSource = query.ToList(); } } dataGridView.Columns["ID"].Visible = false; dataGridView.Columns["Active"].Visible = false; GiveColor(); }
public User ToUserEntity(UserDto udto) { User u = new User(); u.ID = udto.Id; u.FirstName = udto.FirstName; u.LastName = udto.LastName; u.Email = udto.Email; u.Birthdate = DateTime.ParseExact(udto.BirthDate, "dd/MM/yyyy", CultureInfo.InvariantCulture); u.Active = udto.Active; u.Password = udto.Password; using (Session1Entities session1 = new Session1Entities()) { u.RoleID = session1.Roles.Single(r => r.Title.Equals(udto.Role)).ID; u.OfficeID = session1.Offices.Single(o => o.Title.Equals(udto.Office)).ID; } return(u); }
private void QueryData() { using (var db = new Session1Entities()) { var query = from u in db.Users join o in db.Offices on u.OfficeID equals o.ID select new { u.ID, u.RoleID, u.Email, u.Password, u.FirstName, u.LastName, Office = o.Title, u.Birthdate, u.Active }; } }
private void buttonSuspendAccount_Click(object sender, EventArgs e) { if (dataGridView.CurrentCell != null) { using (var session = new Session1Entities()) { // So that there's no LINQ error about // not being able to convert to LINQ expression var currentCellID = int.Parse(dataGridView.CurrentCell.OwningRow.Cells["ID"].Value.ToString()); var result = session.Users.SingleOrDefault(u => u.ID == currentCellID); result.Active = result.Active == true ? false : true; session.SaveChanges(); SetDatagridView(); } } else { MessageBox.Show("Please select a cell/row."); } }
private void AdminMainForm_Load(object sender, EventArgs e) { using (var session = new Session1Entities()) { var query = session.Offices.Select(o => new { o.ID, o.Title }); var result = query.ToList(); // Add All offices option and then sort it based on ID result.Add(new { ID = 0, Title = "All Offices" }); result = result.OrderBy(o => o.ID).ToList(); comboBoxOffice.ValueMember = "ID"; comboBoxOffice.DisplayMember = "Title"; comboBoxOffice.DataSource = result; comboBoxOffice.SelectedIndexChanged += ComboBoxOffice_SelectedIndexChanged; SetDatagridView(); } }
public UserActivityRepository() { session1 = new Session1Entities(); }
public UserRepository() { session1 = new Session1Entities(); }
private void buttonLogin_Click(object sender, EventArgs e) { string username = textBoxUsername.Text; string password = textBoxPassword.Text; password = Hash.MakeMd5(password); using (var session = new Session1Entities()) { var query = session.Users.Where(u => u.Email == username && u.Password == password); var result = query.FirstOrDefault(); if (result != null) { if (result.Active == true) { // Login Success // Administrator = 1, User = 2 if (result.RoleID == 1) { var admin = new AdminMainForm(); // So that if AdminMainForm closed, LoginForm will open again Hide(); admin.ShowDialog(); Show(); } else { var user = new UserMainForm($"{result.FirstName} {result.LastName}", result.ID); // So that if UserMainForm closed, LoginForm will open again Hide(); var timeNow = DateTime.Now.TimeOfDay; session.Activities.Add(new Activity { UserID = result.ID, Date = DateTime.Now.Date, LoginTime = new TimeSpan(timeNow.Hours, timeNow.Minutes, timeNow.Seconds) }); session.SaveChanges(); user.ShowDialog(); Show(); } ClearFields.ClearTextBoxes(this); failedLoginAttempt = 0; } else { MessageBox.Show("User suspended by Administrator!"); failedLoginAttempt++; CheckLoginAttempt(); } } else { // Login Failed MessageBox.Show("Wrong Username or Password!"); failedLoginAttempt++; CheckLoginAttempt(); } } }
public UserManager() { userRepository = new UserRepository(); session1 = new Session1Entities(); }
public OfficeRepository() { session1 = new Session1Entities(); }
public UserActivityManager() { session1 = new Session1Entities(); userActivityRepository = new UserActivityRepository(); }