private void removeItemToolStripMenuItem_Click(object sender, EventArgs e) { if (ItemsDataGridView.SelectedRows.Count == 1) { String ItemName = ItemsDataGridView.SelectedRows[0].Cells["Title"].Value.ToString(); String ItemAuthor = ItemsDataGridView.SelectedRows[0].Cells["Author"].Value.ToString(); try { using (LibraryApp.Models.LibraryDB context = new LibraryDB()) { if (context.BorrowAbles.Any(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor)) { BorrowAble ToRemove = context.BorrowAbles.Single(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor); context.BorrowAbles.Remove(ToRemove); } else if (context.NonBorrowAbles.Any(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor)) { NonBorrowAble ToRemove = context.NonBorrowAbles.Single(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor); context.NonBorrowAbles.Remove(ToRemove); } context.SaveChanges(); this.DataFill(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
public void LoadDataForEdit(String ItemName, String ItemAuthor) { try { using (LibraryApp.Models.LibraryDB context = new LibraryDB()) { if (context.BorrowAbles.Any(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor)) { BorrowAble ToEdit = context.BorrowAbles.Single(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor); TitleTextbox.Text = context.Titles.FirstOrDefault(s => s.TitleID == ToEdit.TitleID).TitleName; ISBNTextbox.Text = ToEdit.ISBN; OverviewTextbox.Text = ToEdit.Overview; PublisherTextbox.Text = context.Publishers.FirstOrDefault(s => s.PublisherName == ToEdit.PublisherName).PublisherName; GenreCombobox.SelectedIndex = GenreCombobox.Items.IndexOf(context.Genres.FirstOrDefault(g => g.GenreName == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).GenreName).GenreName); LanguageCombobox.SelectedIndex = LanguageCombobox.Items.IndexOf(context.Languages.FirstOrDefault(l => l.LanguageName == ToEdit.LanguageName).LanguageName); TypeCombobox.SelectedIndex = TypeCombobox.Items.IndexOf(context.Types.FirstOrDefault(t => t.TypeName == ToEdit.TypeName).TypeName); FormatCombobox.SelectedIndex = FormatCombobox.Items.IndexOf(context.Formats.FirstOrDefault(f => f.FormatName == ToEdit.FormatName).FormatName); CategoryCombobox.SelectedIndex = CategoryCombobox.Items.IndexOf("Borrowable"); PriceTextbox.Text = ToEdit.Price.ToString(); PublishedDateTimePicker.Value = ToEdit.PublishedDate; AuthorNameTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonFirstName; AuthorSurnameTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonLastName; AuthorBioTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).Biography; AuthorBirthdayTimePicker.Value = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonDateOfBirth; } else if (context.NonBorrowAbles.Any(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor)) { NonBorrowAble ToEdit = context.NonBorrowAbles.Single(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor); TitleTextbox.Text = context.Titles.FirstOrDefault(s => s.TitleID == ToEdit.TitleID).TitleName; ISBNTextbox.Text = ToEdit.ISBN; OverviewTextbox.Text = ToEdit.Overview; PublisherTextbox.Text = context.Publishers.FirstOrDefault(s => s.PublisherName == ToEdit.PublisherName).PublisherName; GenreCombobox.SelectedIndex = GenreCombobox.Items.IndexOf(context.Genres.FirstOrDefault(g => g.GenreName == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).GenreName).GenreName); LanguageCombobox.SelectedIndex = LanguageCombobox.Items.IndexOf(context.Languages.FirstOrDefault(l => l.LanguageName == ToEdit.LanguageName).LanguageName); TypeCombobox.SelectedIndex = TypeCombobox.Items.IndexOf(context.Types.FirstOrDefault(t => t.TypeName == ToEdit.TypeName).TypeName); FormatCombobox.SelectedIndex = FormatCombobox.Items.IndexOf(context.Formats.FirstOrDefault(f => f.FormatName == ToEdit.FormatName).FormatName); CategoryCombobox.SelectedIndex = CategoryCombobox.Items.IndexOf("Borrowable"); PriceTextbox.Text = ToEdit.Price.ToString(); PublishedDateTimePicker.Value = ToEdit.PublishedDate; AuthorNameTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonFirstName; AuthorSurnameTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonLastName; AuthorBioTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).Biography; AuthorBirthdayTimePicker.Value = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonDateOfBirth; } TitleTextbox.ReadOnly = true; ISBNTextbox.ReadOnly = true; PublisherTextbox.ReadOnly = true; LanguageCombobox.DropDownStyle = ComboBoxStyle.Simple; TypeCombobox.DropDownStyle = ComboBoxStyle.Simple; FormatCombobox.DropDownStyle = ComboBoxStyle.Simple; PriceTextbox.ReadOnly = true; PublishedDateTimePicker.Enabled = false; AuthorNameTextbox.ReadOnly = true; AuthorSurnameTextbox.ReadOnly = true; AuthorBirthdayTimePicker.Enabled = false; TitleTextbox.BorderStyle = BorderStyle.None; ISBNTextbox.BorderStyle = BorderStyle.None; PublisherTextbox.BorderStyle = BorderStyle.None; PriceTextbox.BorderStyle = BorderStyle.None; AuthorNameTextbox.BorderStyle = BorderStyle.None; AuthorSurnameTextbox.BorderStyle = BorderStyle.None; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }