Example #1
0
        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.");
            }
        }
Example #2
0
 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.");
     }
 }