private void SaveExecute() { try { tblEmploye newEmploye = new tblEmploye(); newEmploye.FirstName = Employe.FirstName; newEmploye.Surname = Employe.Surname; newEmploye.JMBG = Employe.JMBG; newEmploye.Salary = Convert.ToInt32(Employe.Salary); newEmploye.Account = Employe.Account; newEmploye.Pasword = Employe.Pasword; newEmploye.Username = Employe.Username; newEmploye.Position = Employe.Position; newEmploye.Email = Employe.Email; string birthdate = cmvm.CalculateBirth(Employe.JMBG); if (KeyCheck(newEmploye.JMBG) == true && PasswordCheck(newEmploye.Pasword) == true && UsernameCheck(newEmploye.Username) == true && NumbersOnly(newEmploye.JMBG) == true) { newEmploye.DateOfBirth = cmvm.CalculateBirth(Employe.JMBG); context.tblEmployes.Add(newEmploye); context.SaveChanges(); MessageBox.Show("Employe is created"); ListEmploye = EmpList(); } else { MessageBox.Show("JMBG or password or username already exist"); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException MessageBox.Show(message); raise = new InvalidOperationException(message, raise); } } throw raise; } }
private void DeleteExecute() { try { using (Entity2 context = new Entity2()) { tblEmploye employeToDelete = (from x in context.tblEmployes where x.EmployeID == Employe.EmployeID select x).First(); List <int> foreignKeysList = new List <int>(); List <tblManager> managersList = context.tblManagers.ToList(); for (int i = 0; i < managersList.Count; i++) { foreignKeysList.Add(managersList[i].EmployeID.GetValueOrDefault()); } MessageBoxResult mbr = MessageBox.Show("Are you sure you want to delete employe?", "Delete confirmation", MessageBoxButton.YesNo); if (mbr == MessageBoxResult.Yes) { if (foreignKeysList.Contains(employeToDelete.EmployeID)) { tblManager managerToDelete = (from x in context.tblManagers where x.EmployeID == Employe.EmployeID select x).First(); context.tblManagers.Remove(managerToDelete); } context.tblEmployes.Remove(employeToDelete); context.SaveChanges(); MessageBox.Show("Employe is deleted"); ListEmploye = EmpList(); Employe = new tblEmploye(); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void SaveExecute() { try { tblEmploye newEmploye = new tblEmploye(); newEmploye.FirstName = Employe.FirstName; newEmploye.Surname = Employe.Surname; newEmploye.JMBG = Employe.JMBG; newEmploye.Salary = Convert.ToInt32(Employe.Salary); newEmploye.Account = Employe.Account; newEmploye.Pasword = Employe.Pasword; newEmploye.Username = Employe.Username; newEmploye.Position = Employe.Position; newEmploye.Email = Employe.Email; string birthdate = CalculateBirth(Employe.JMBG); CreateManager cm = new CreateManager(); cm.tbJMBG.Text = ""; //method checks if jmbg already exists in database AND ONLY IF NOT proceeds to save employe if (KeyCheck(newEmploye.JMBG) == true && PasswordCheck(newEmploye.Pasword) == true && UsernameCheck(newEmploye.Username) == true && NumbersOnly(newEmploye.JMBG) == true) { newEmploye.DateOfBirth = CalculateBirth(Employe.JMBG); context.tblEmployes.Add(newEmploye); context.SaveChanges(); tblManager newManager = new tblManager(); newManager.EmployeID = newEmploye.EmployeID; newManager.SectorID = Sector.SectorID; newManager.LevelID = Level.LevelID; context.tblManagers.Add(newManager); context.SaveChanges(); MessageBox.Show("Manager is created."); if (!worker.IsBusy) { worker.RunWorkerAsync(); } } else { MessageBox.Show("JMBG or password or username already exists"); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException MessageBox.Show(message); raise = new InvalidOperationException(message, raise); } } throw raise; } }