private void buttonUpdate2_Click(object sender, EventArgs e) { DateTime?release = null; int id = Convert.ToInt32(textBoxId2.Text); Publisher publisher = null; Book book = _database.Book.FirstOrDefault(x => x.Id == id); book.bookname = textBoxBook2.Text; book.Genre.genrename = comboBoxGenre2.Text; if (textBoxPublisher2.Text != "") { publisher = new Publisher { publishername = textBoxPublisher2.Text }; } book.Publisher = publisher; ConvertStringToDate.ConvertDate(textBoxDate2.Text, ref release); book.releasedate = release; _database.SaveChanges(); this.Close(); }
private void buttonUpdateBook_Click(object sender, EventArgs e) { if (textBoxDate.Text == "dd/mm/yyyy format") { textBoxDate.Text = ""; } if (string.IsNullOrEmpty(textBoxBook.Text) || string.IsNullOrEmpty(textBoxAuthor.Text)) { MessageBox.Show("Please first specify a book to update"); return; } DateTime? release = null; Publisher publisher = null; Author author = new Author { authorname = textBoxAuthor.Text }; Genre genre = _database.Genre.FirstOrDefault(x => x.genrename == comboBoxGenres.Text); if (textBoxPublisher.Text != "") { publisher = new Publisher { publishername = textBoxPublisher.Text }; } string date = textBoxDate.Text; ConvertStringToDate.ConvertDate(date, ref release); int id = Convert.ToInt32(textBoxId.Text); //Book to update. Book book = _database.Book.FirstOrDefault(x => x.Id == id); if (book != null) { book.bookname = textBoxBook.Text; book.Author = author; book.Genre = genre; book.Publisher = publisher; book.releasedate = release; //Reload the data. _database.SaveChanges(); Form1_Load(sender, e); MessageBox.Show("Successfully changed a book in the library"); } }
private void buttonAddBook_Click(object sender, EventArgs e) { if (textBoxDate.Text == "dd/mm/yyyy format") { textBoxDate.Text = ""; } //Validate required fields.. if (string.IsNullOrEmpty(textBoxBook.Text) || string.IsNullOrEmpty(textBoxAuthor.Text)) { MessageBox.Show("Please input required fields to add, bookname and Author"); return; } DateTime? release = null; Publisher publisher = null; Author author = new Author { authorname = textBoxAuthor.Text }; _database.Author.Add(author); Genre genre = _database.Genre.FirstOrDefault(x => x.genrename == comboBoxGenres.Text); if (textBoxPublisher.Text != "") { publisher = new Publisher { publishername = textBoxPublisher.Text }; } //Try to add a new book ConvertStringToDate.ConvertDate(textBoxDate.Text, ref release); //Add new book Book book = new Book(); book.Author = author; book.bookname = textBoxBook.Text; book.Genre = genre; book.Publisher = publisher; book.releasedate = release; _database.Book.Add(book); _database.SaveChanges(); Form1_Load(sender, e); MessageBox.Show("Successfully added a new book to the library"); }