public void Insert() { //Inserts the books from the data grid into the db try { Model1Container2 db = new Model1Container2(); foreach (var book in booksRead) { BookTBL newBook = new BookTBL() { Author = $"{book.Author}", Title = $"{book.Title}", AuthorTBLId = 1 }; db.BookTBLs.Add(newBook); db.SaveChanges(); } } catch (NullReferenceException nre) { MessageBox.Show(nre.Message); } catch (Exception) { MessageBox.Show("An error inserting into the database occurred"); } }
private void dataGrid_read_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { //select the book as an implicit type var bookSelected = dataGrid_read.SelectedItem; ResetDescriptionStyles(); //checks the type of object the book is //this problem arose from making the db in a the EF Designer //and not code first //this created two similar book classes to work with //instead of one if (bookSelected is Book) { Book bR = dataGrid_read.SelectedItem as Book; GetBookInfo(bR.Title + " " + bR.Author); } else if (bookSelected is BookTBL) { BookTBL b = dataGrid_read.SelectedItem as BookTBL; GetBookInfo(b.Title + " " + b.Author); } }