//===================THIS METHOD IS FOR EDIT RECORDS IN DIRECTIONS TABLE============== public void EditDir(string newDirName) { if (Dir.DirectionName == "---") { dialogService.ShowMessage("Нельзя редактировать эту запись"); return; } Dir.DirectionName = newDirName; using (StudentuConteiner db = new StudentuConteiner()) { try { var res1 = db.Directions.Find(Dir.DirectionId); if (res1 != null) { //changing DB res1.DirectionName = Dir.DirectionName.ToLower(); Dir.DirectionName.Trim(); db.SaveChanges(); DirRecords.Clear(); LoadDirectionsData(); } } catch (ArgumentNullException ex) { dialogService.ShowMessage(ex.Message); } catch (OverflowException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.SqlClient.SqlException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.Entity.Core.EntityCommandExecutionException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.Entity.Core.EntityException ex) { dialogService.ShowMessage(ex.Message); } } }
//load data array from "Directions" table public void LoadDirectionsData() { using (StudentuConteiner db = new StudentuConteiner()) { try { var list = db.Directions.ToList <Direction>(); foreach (var item in list) { DirRecords.Add( new Direction { DirectionId = item.DirectionId, DirectionName = item.DirectionName }); } Dir = DirRecords[0]; SelectedDir2 = Dir; } catch (ArgumentNullException ex) { dialogService.ShowMessage(ex.Message); } catch (OverflowException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.SqlClient.SqlException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.Entity.Core.EntityCommandExecutionException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.Entity.Core.EntityException ex) { dialogService.ShowMessage(ex.Message); } } }
//===================THIS METHOD IS FOR DELETE RECORDS IN DIRECTIONS TABLE============== public void DeleteDir() { using (StudentuConteiner db = new StudentuConteiner()) { try { var res = db.Orderlines; if (Dir.DirectionName != "---" && !CheckRecordBeforDelete(Dir)) { if (dialogService.YesNoDialog("Точно нужно удалить эту запись?") == true) { //changing DB //we find all the records in which we have the desired Id and make a replacement foreach (OrderLine order in res) { if (order.Direction.DirectionId == Dir.DirectionId) { order.Direction = db.Directions.Find(new Direction() { DirectionId = 1 }.DirectionId); } } db.Directions.Remove(db.Directions.Find(Dir.DirectionId)); db.SaveChanges(); //changing collection AuthorDirections.Remove(Dir); DirRecords.Remove(Dir); Dir = DirRecords[0]; SelectedDir2 = Dir; } } else { dialogService.ShowMessage("Нельзя удалить эту запись"); } } catch (ArgumentNullException ex) { dialogService.ShowMessage(ex.Message); } catch (OverflowException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.SqlClient.SqlException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.Entity.Core.EntityCommandExecutionException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.Entity.Core.EntityException ex) { dialogService.ShowMessage(ex.Message); } } }
//===================THIS METHOD IS FOR ADD RECORDS IN DIRECTIONS TABLE============== public void AddDir(string newDirName) { if (Dir != null) { Dir.DirectionName = newDirName; } else { Dir = new Direction() { DirectionName = newDirName } }; using (StudentuConteiner db = new StudentuConteiner()) { try { var res1 = db.Directions.Any(o => o.DirectionName == Dir.DirectionName); if (!res1) { if (!string.IsNullOrEmpty(Dir.DirectionName) || Dir.DirectionName != "---") { Dir.DirectionName = Dir.DirectionName.ToLower(); Dir.DirectionName.Trim(); if (Dir.DirectionName[0] == ' ') { dialogService.ShowMessage("Нельзя добавить пустую строку"); return; } db.Directions.Add(Dir); db.SaveChanges(); DirRecords.Clear(); LoadDirectionsData(); Dir = new Direction(); SelectedDir2 = Dir; } else { return; } } else { dialogService.ShowMessage("Уже есть такое название в базе данных"); } } catch (ArgumentNullException ex) { dialogService.ShowMessage(ex.Message); } catch (OverflowException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.SqlClient.SqlException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.Entity.Core.EntityCommandExecutionException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.Entity.Core.EntityException ex) { dialogService.ShowMessage(ex.Message); } } }