private async void Create_UpdateSpecializationButton_Click(object sender, RoutedEventArgs e)
 {
     using (UniversityEntities en = new UniversityEntities())
     {
         if (Create_UpdateSpecializationButton.Content.ToString() == "Create")
         {
             en.Specializations.Add(new Specialization {
                 Name = Create_UpdateSpecializatinTextBox.Text
             });
         }
         else if (Create_UpdateSpecializationButton.Content.ToString() == "Update")
         {
             int outId;
             if (!int.TryParse(UpdateSpecializatinID.Text, out outId))
             {
                 return;
             }
             en.Specializations.Where(q => q.Id == outId).FirstOrDefault().Name = Create_UpdateSpecializatinTextBox.Text;
         }
         await en.SaveChangesAsync();
     }
 }
 private async void Create_UpdateSpecializationButton_Click(object sender, RoutedEventArgs e)
 {
     using (UniversityEntities en = new UniversityEntities())
     {
         int AOH  = -1;
         int SpId = -1;
         if (Create_UpdateSpecializationButton.Content.ToString() == "Create")
         {
             if (!int.TryParse(Create_UpdateSpecializatinTextBoxAmounthOfHour.Text, out AOH) ||
                 !int.TryParse(Create_UpdateSpecializatinTextBoxSpecialization_Id.Text, out SpId))
             {
                 MessageBox.Show("Inkorect Inputed data");
                 return;
             }
             en.Subjects.Add(new Subject {
                 Name = Create_UpdateSpecializatinTextBoxName.Text, AmountOfHour = AOH, Specialization_Id = SpId
             });
         }
         else if (Create_UpdateSpecializationButton.Content.ToString() == "Update")
         {
             int outId;
             if ((!int.TryParse(UpdateSpecializatinID.Text, out outId) ||
                  !int.TryParse(Create_UpdateSpecializatinTextBoxAmounthOfHour.Text, out AOH) ||
                  !int.TryParse(Create_UpdateSpecializatinTextBoxSpecialization_Id.Text, out SpId)) &&
                 en.Subjects.Where(q => q.Id == outId).Count() > 0)
             {
                 MessageBox.Show("Inkorect Inputed data");
                 return;
             }
             var sub = en.Subjects.Where(q => q.Id == outId).FirstOrDefault();
             sub.Name              = Create_UpdateSpecializatinTextBoxName.Text;
             sub.AmountOfHour      = AOH;
             sub.Specialization_Id = SpId;
         }
         await en.SaveChangesAsync();
     }
 }
Пример #3
0
        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            using (UniversityEntities en = new UniversityEntities())
            {
                List <int> DelId = new List <int>();
                int        id;
                switch (TabControl.SelectedIndex)
                {
                case 0:
                    foreach (var i in StusentListView.SelectedItems)
                    {
                        try
                        {
                            id = int.Parse(i.ToString().Remove(i.ToString().IndexOf("#")));
                        }
                        catch
                        {
                            continue;
                        }
                        var res = en.Students.Where(q => q.Id == id)?.FirstOrDefault();
                        if (res == null)
                        {
                            continue;
                        }
                        en.Students.Remove(res);
                    }
                    break;

                case 1:
                    foreach (var i in SpecializationListView.SelectedItems)
                    {
                        try
                        {
                            id = int.Parse(i.ToString().Remove(i.ToString().IndexOf("#")));
                        }
                        catch
                        {
                            continue;
                        }
                        var res = en.Specializations.Where(q => q.Id == id).FirstOrDefault();
                        if (res == null)
                        {
                            continue;
                        }
                        en.Specializations.Remove(res);
                    }
                    break;

                case 2:
                    foreach (var i in SubjectListView.SelectedItems)
                    {
                        try
                        {
                            id = int.Parse(i.ToString().Remove(i.ToString().IndexOf("#")));
                        }
                        catch
                        {
                            continue;
                        }
                        var res = en.Subjects.Where(q => q.Id == id)?.FirstOrDefault();
                        if (res == null)
                        {
                            continue;
                        }
                        en.Subjects.Remove(res);
                    }
                    break;
                }
                try
                {
                    await en.SaveChangesAsync();
                }
                catch
                {
                    MessageBox.Show("Selected item(s) is not deleted because is foreign key use");
                }
            }
        }