private void btn_remove_Click(object sender, EventArgs e) { //Remove a user-chosen book at a user-chosen bookstore. using (var db = new LAM_Lab2Context()) { var result = MessageBox.Show("Are you sure?", "Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { try { var item = comboBox_bookstore.SelectedValue; var id = Convert.ToInt32(item); var bookInStock = new StockBalance() { BookStoresId = id, BooksIsbn13 = comboBox_book.SelectedValue.ToString() }; db.StockBalances.Remove(bookInStock); db.SaveChanges(); } catch (Exception ee) { MessageBox.Show($"{ee}", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning); } dataGridView_storeData.DataSource = sqlResult(); } } }
private void btn_Save_Click(object sender, EventArgs e) { using (var db = new LAM_Lab2Context()) { // Adds a new book (from combobox_book) to a bookstore. // Shows errormessage if fail. try { var item = comboBox_bookstore.SelectedValue; var id = Convert.ToInt32(item); var bookInStock = new StockBalance() { NumberOfItems = Int32.Parse(tb_nrOfItems.Text), BookStoresId = id, BooksIsbn13 = comboBox_book.SelectedValue.ToString() }; db.StockBalances.Add(bookInStock); db.SaveChanges(); } catch (Exception ee) { MessageBox.Show($"{ee}", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning); } dataGridView_storeData.DataSource = sqlResult(); } }
private void button_saveUpdateBook_Click(object sender, EventArgs e) { using (var db = new LAM_Lab2Context()) { try { //Updates the book in database with the changes the user made. var book = db.Books.Find(tb_ISBN.Text); book.Isbn13 = tb_ISBN.Text; book.Title = tb_title.Text; book.Price = decimal.Parse(tb_Price.Text); book.Language = tb_Language.Text; book.Active = checkBox_activeBook.Checked; db.Books.Update(book); db.SaveChanges(); dataGridView_books.DataSource = db.Books.ToList(); } catch (Exception ee) { MessageBox.Show($"{ee}"); } } }
private void btn_Update_Click_1(object sender, EventArgs e) { //Updates the number of books of a user-chosen book at a user-chosen bookstore. using (var db = new LAM_Lab2Context()) { try { var item = comboBox_bookstore.SelectedValue; var id = Convert.ToInt32(item); var bookInStock = new StockBalance() { NumberOfItems = Int32.Parse(tb_nrOfItems.Text), BookStoresId = id, BooksIsbn13 = comboBox_book.SelectedValue.ToString() }; db.StockBalances.Update(bookInStock); db.SaveChanges(); } catch (Exception ee) { MessageBox.Show($"{ee}", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning); } dataGridView_storeData.DataSource = sqlResult(); } }
private void button1_Click(object sender, EventArgs e) { using (var db = new LAM_Lab2Context()) { var result = MessageBox.Show("Are you sure?", "Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { try { //Removes authorID and booksIsbn from the database so they are no longer "connected". var item = cb_Authors.SelectedValue; var id = Convert.ToInt32(item); var conn = new AuthorsBook() { AuthorsId = id, BooksIsbn13 = cb_Books.SelectedValue.ToString() }; db.AuthorsBooks.Remove(conn); db.SaveChanges(); MessageBox.Show("Connecton deleted.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception) { MessageBox.Show("Author not associated with that book.\n" + "No changes made.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } }
private void btn_SaveConn_Click(object sender, EventArgs e) { using (var db = new LAM_Lab2Context()) { try { //Saves authorID and booksIsbn to the database so they are "connected". var item = cb_Authors.SelectedValue; var id = Convert.ToInt32(item); var conn = new AuthorsBook() { AuthorsId = id, BooksIsbn13 = cb_Books.SelectedValue.ToString() }; db.AuthorsBooks.Add(conn); db.SaveChanges(); MessageBox.Show("Author 'connected' to book", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ee) { MessageBox.Show("Author already 'connected' to book", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
private void btn_Add_Click(object sender, EventArgs e) { using (var db = new LAM_Lab2Context()) { //Adds a new author with user's input. try { int searchID = authId; int index = -1; index = (dataGridView_Authors.Rows.Cast <DataGridViewRow>() .Where(r => r.Cells[0].Value.Equals(authId)) .Select(r => r.Index)).FirstOrDefault(); if (index > 0) { MessageBox.Show("Author with that ID already exists", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { var author = new Author { AFirstName = tb_Firstname.Text, ALastName = tb_Lastname.Text, BirthDate = DateTime.Parse(tb_Birthdate.Text), Active = checkbox_Active.Checked }; if (string.IsNullOrWhiteSpace(tb_Firstname.Text) || string.IsNullOrWhiteSpace(tb_Lastname.Text)) { MessageBox.Show("First- or lastname can't be empty.", "Missing information", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { db.Authors.Add(author); db.SaveChanges(); dataGridView_Authors.DataSource = db.Authors.ToList(); MessageBox.Show("Author added successfully.", "New Author" , MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception ee) { MessageBox.Show($"{ee}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void btn_Save_Click(object sender, EventArgs e) { using (var db = new LAM_Lab2Context()) { try { //Checks if ISBS is already in the dataGridView and aborts the user's action if match found. string searchISBN = tb_ISBN.Text; int index = -1; index = (dataGridView_books.Rows.Cast <DataGridViewRow>() .Where(r => r.Cells[0].Value.Equals(searchISBN)) .Select(r => r.Index)).FirstOrDefault(); if (index > 0) { MessageBox.Show("Book with that ISBN is already in the database.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { //If no matching ISBN is found, adds new book to the database. var book = new Book { Isbn13 = tb_ISBN.Text, Title = tb_title.Text, Price = decimal.Parse(tb_Price.Text), Published = dateTimePicker1.Value, Language = tb_Language.Text, Active = checkBox_activeBook.Checked }; db.Books.Add(book); db.SaveChanges(); dataGridView_books.DataSource = db.Books.ToList(); MessageBox.Show("Book added successfully", "New book", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ee) { MessageBox.Show($"{ee}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void btn_Save_Click(object sender, EventArgs e) { //Updates chosen author with user's altered input. using (var db = new LAM_Lab2Context()) { try { var author = db.Authors.Find(authId); author.AFirstName = tb_Firstname.Text; author.ALastName = tb_Lastname.Text; author.BirthDate = DateTime.Parse(tb_Birthdate.Text); author.Active = checkbox_Active.Checked; if (string.IsNullOrEmpty(tb_Firstname.Text) || string.IsNullOrWhiteSpace(tb_Lastname.Text)) { MessageBox.Show("First- or lastname can't be empty.", "Missing information", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { db.Authors.Update(author); db.SaveChanges(); dataGridView_Authors.DataSource = db.Authors.ToList(); MessageBox.Show("Author updated successfully.", "Updated Author", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ee) { MessageBox.Show($"{ee}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }