/// <summary> /// Saves the created Worker in database /// </summary> private void SaveWorker() { try { using (WorkersDbEntities db = new WorkersDbEntities()) { if (ValideteJMBG(Worker.JMBG)) { if (counter == 0) { Worker.Gender = Gender; db.tblWorkers.Add(Worker); db.SaveChanges(); counter++; } WriteWorker(); } else { MessageBox.Show("Invalid JMBG"); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } }
/// <summary> /// Deletes Worker from database /// </summary> private void WorkerDelete(object sender, DoWorkEventArgs e) { try { using (WorkersDbEntities db = new WorkersDbEntities()) { Thread.Sleep(2000); tblWorker worker = db.tblWorkers.Where(x => x.Id == viewWorker.Id).FirstOrDefault(); db.tblWorkers.Remove(worker); db.SaveChanges(); AllInfoAboutWorker = GetAllInfoAboutWorkers(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } }
/// <summary> /// Gets all Locations form txt file and write them into the database /// </summary> private void WriteLocations() { using (WorkersDbEntities db = new WorkersDbEntities()) { List <tblLocation> locations = new List <tblLocation>(); foreach (tblLocation location1 in db.tblLocations) { locations.Add(location1); } if (locations.Count == 0) { string location = @"~\..\..\..\Locations.txt"; if (File.Exists(location)) { string[] allLocations = File.ReadAllLines(location); foreach (string oneLocation in allLocations) { string[] separeted = oneLocation.Split(','); string address = separeted[0]; string city = separeted[1]; string country = separeted[2]; tblLocation newLocation = new tblLocation(); newLocation.Address = address; newLocation.City = city; newLocation.Country = country; using (WorkersDbEntities dbase = new WorkersDbEntities()) { dbase.tblLocations.Add(newLocation); dbase.SaveChanges(); } } } else { System.Diagnostics.Debug.WriteLine("File not found"); } } } }
/// <summary> /// Saves created Worker to the database /// </summary> private void SaveExecute() { tblLocation location = new tblLocation(); string[] fullLocation = Location.Split(','); string address = fullLocation[0]; try { using (WorkersDbEntities db = new WorkersDbEntities()) { location = db.tblLocations.Where(x => x.Address == address).FirstOrDefault(); Worker.FKLocation = location.Id; Worker.Gender = Gender; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } tblSector sector = new tblSector(); sector.Sector = Sector; List <tblSector> sectors = new List <tblSector>(); try { using (WorkersDbEntities db = new WorkersDbEntities()) { foreach (tblSector dbSector in db.tblSectors) { sectors.Add(dbSector); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } if (sectors.Count == 0) { try { using (WorkersDbEntities db = new WorkersDbEntities()) { db.tblSectors.Add(sector); db.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } try { using (WorkersDbEntities db = new WorkersDbEntities()) { tblSector sector1 = db.tblSectors.Where(x => x.Sector == sector.Sector).FirstOrDefault(); Worker.FKSector = sector1.Id; db.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } } else { for (int i = 0; i < sectors.Count; i++) { if (sectors[i].Sector == sector.Sector) { Worker.FKSector = sectors[i].Id; } else if (i == sectors.Count - 1 && sectors[i].Sector != sector.Sector) { try { using (WorkersDbEntities db = new WorkersDbEntities()) { db.tblSectors.Add(sector); db.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } try { using (WorkersDbEntities db = new WorkersDbEntities()) { tblSector sector1 = db.tblSectors.Where(x => x.Sector == sector.Sector).FirstOrDefault(); Worker.FKSector = sector1.Id; db.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message.ToString()); } } else { continue; } } } SaveWorker(); }