private void UpdateSpecializatinID_TextChanged(object sender, TextChangedEventArgs e) { using (UniversityEntities en = new UniversityEntities()) { int outId; if (!int.TryParse(((TextBox)sender).Text.ToString(), out outId)) { return; } Create_UpdateSpecializatinTextBox.Text = en.Specializations.Where(q => q.Id == outId)?.FirstOrDefault()?.Name; } }
private void Button_Click_3(object sender, RoutedEventArgs e) { switch (TabControl.SelectedIndex) { case 0: using (UniversityEntities en = new UniversityEntities()) { StusentListView.Items.Clear(); StusentListView.Items.Add("Id | Name | Surname | Phone | Address | Specialization Id"); foreach (var i in en.Students) { StusentListView.Items.Add(i.Id + "# | " + i.Name + " | " + i.Surname + " | " + i.Phone + " | " + i.Address + " | " + i.Specialization_Id); } } break; case 1: using (UniversityEntities en = new UniversityEntities()) { SpecializationListView.Items.Clear(); SpecializationListView.Items.Add("Id | Name"); foreach (var i in en.Specializations) { SpecializationListView.Items.Add(i.Id + "# | " + i.Name); } } break; case 2: using (UniversityEntities en = new UniversityEntities()) { SubjectListView.Items.Clear(); SubjectListView.Items.Add("Id | Name | Amount Of Hour | Specialization Id"); foreach (var i in en.Subjects) { SubjectListView.Items.Add(i.Id + "# | " + i.Name + " | " + i.AmountOfHour + " | " + i.Specialization_Id); } } break; } }
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(); } }
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"); } } }