private void Window_Loaded(object sender, RoutedEventArgs e) { using (WpfApplicationEntity.API.MyDBContext objectMyDBContext = new WpfApplicationEntity.API.MyDBContext()) { WpfApplicationEntity.API.Action naz = new WpfApplicationEntity.API.Action(); List <string> list = new List <string>(); var actions = objectMyDBContext.Action_for_the_needys.ToList(); foreach (var item in actions) { list.Add(item.Name); } comboBlockAddEditActionForTheNeedy.ItemsSource = list; List <string> list1 = new List <string>(); var actions1 = objectMyDBContext.Type_Actions.ToList(); foreach (var item in actions1) { list1.Add(item.Name); } comboBlockAddEditTypeAction.ItemsSource = list1; if (add_edit == false) { ButtonAddEditGroup.Content = "Сохранить"; naz = objectMyDBContext.Actions.Find(EditID); Starttime.Text = naz.StarTime; Endtime.Text = naz.EndTime; Dateofthe.Text = naz.TheDateOfThe; Numberofvolunteer.Text = naz.NumberOfVolonteers.ToString(); Numberofneedy.Text = naz.NumberOfNeeds.ToString(); comboBlockAddEditActionForTheNeedy.Text = naz.Action_for_the_needy.Name; comboBlockAddEditTypeAction.Text = naz.Type_Action.Name; } } }
private void ButtonAddEditAction_Click(object sender, RoutedEventArgs e) { using (WpfApplicationEntity.API.MyDBContext objectMyDBContext = new WpfApplicationEntity.API.MyDBContext()) { if (this.add_edit == true) { if (Starttime.Text != string.Empty && Endtime.Text != string.Empty && Dateofthe.Text != string.Empty && Numberofvolunteer.Text != string.Empty && Numberofneedy.Text != string.Empty && comboBlockAddEditActionForTheNeedy.SelectedIndex != -1 && comboBlockAddEditTypeAction.SelectedIndex != -1) { WpfApplicationEntity.API.Action objectAction = new WpfApplicationEntity.API.Action(); objectAction.StarTime = Starttime.Text; objectAction.EndTime = Endtime.Text; objectAction.TheDateOfThe = Dateofthe.Text; objectAction.NumberOfVolonteers = Convert.ToInt32(Numberofvolunteer.Text); objectAction.NumberOfNeeds = Convert.ToInt32(Numberofneedy.Text); objectAction.Action_for_the_needy = GetActionForTheNeedy(objectMyDBContext.Action_for_the_needys.ToList()); objectAction.Type_Action = GetTypeAction(objectMyDBContext.Type_Actions.ToList()); try { objectMyDBContext.Actions.Add(objectAction); objectMyDBContext.SaveChanges(); MessageBox.Show("Акция добавлена"); this.DialogResult = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "ОШИБКА", MessageBoxButton.OK, MessageBoxImage.Error); } } else { MessageBox.Show("Заполните все поля!", "Ошибка!"); this.DialogResult = false; } } else { var result = objectMyDBContext.Actions.Find(EditID); result.StarTime = Starttime.Text; result.EndTime = Endtime.Text; result.TheDateOfThe = Dateofthe.Text; result.NumberOfVolonteers = Convert.ToInt32(Numberofvolunteer.Text); result.NumberOfNeeds = Convert.ToInt32(Numberofneedy.Text); result.Action_for_the_needy = GetActionForTheNeedy(objectMyDBContext.Action_for_the_needys.ToList()); result.Type_Action = GetTypeAction(objectMyDBContext.Type_Actions.ToList()); } objectMyDBContext.SaveChanges(); } this.Close(); }