예제 #1
0
        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();
            }
        }
예제 #2
0
        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();
                }
            }
        }
예제 #3
0
        private void Stockbalance_Load(object sender, EventArgs e)
        {
            using (var db = new LAM_Lab2Context())
            {
                //Fills the dataGridView with data from the database.
                dataGridView_storeData.DataSource = sqlResult();


                //Shows the different bookstores by name
                SqlDataAdapter da2 = new SqlDataAdapter("SELECT ID, Name FROM BookStores", jsonConn);
                DataTable      dt2 = new DataTable();
                da2.Fill(dt2);
                comboBox_bookstore.DataSource    = dt2;             // setting the datasource property of combobox
                comboBox_bookstore.DisplayMember = "Name";          // Display Member which will display on screen
                comboBox_bookstore.ValueMember   = "ID";            // Fetching the ID for the bookStore


                //Shows the different books by title
                SqlDataAdapter da = new SqlDataAdapter("SELECT ISBN13, Title FROM Books WHERE Active = 1", jsonConn);
                DataTable      dt = new DataTable();
                da.Fill(dt);
                comboBox_book.DataSource    = dt;                   // setting the datasource property of combobox
                comboBox_book.DisplayMember = "Title";              // Display Member which will display on screen
                comboBox_book.ValueMember   = "ISBN13";             // Fetching the id for the Title
            }
        }
예제 #4
0
        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 dataGridView_Authors_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Updates the textboxes and other controls with information from the dataGridView.
            //And enables the Update-button if the user wants to update an author.
            try
            {
                using (var db = new LAM_Lab2Context())
                {
                    string id    = dataGridView_Authors.Rows[e.RowIndex].Cells[0].Value.ToString();
                    int    idInt = int.Parse(id);

                    var author = db.Authors.Find(idInt);

                    authId                  = author.Id;
                    tb_Firstname.Text       = author.AFirstName;
                    tb_Lastname.Text        = author.ALastName;
                    tb_Birthdate.Value      = author.BirthDate;
                    checkbox_Active.Checked = author.Active;


                    this.btn_Save.Enabled = true;
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show($"{ee}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
예제 #6
0
        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}");
                }
            }
        }
예제 #7
0
        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);
                    }
                }
            }
        }
예제 #8
0
        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);
                }
            }
        }
예제 #9
0
        private void BaseForm_Load(object sender, EventArgs e)
        {
            using (var db = new LAM_Lab2Context())
            {
                var booksData = from book in db.Books
                                join AuthorsBook in db.AuthorsBooks
                                on book.Isbn13 equals AuthorsBook.BooksIsbn13
                                join Author in db.Authors
                                on AuthorsBook.AuthorsId equals Author.Id
                                where book.Active == true
                                select new
                {
                    Author.AFirstName,
                    Author.ALastName,
                    book.Title
                };

                if (booksData != null)
                {
                    if (booksData.Count() > 0)
                    {
                        GridView_base.DataSource            = booksData.ToList();
                        GridView_base.Columns[0].HeaderText = "Author Firstname";
                        GridView_base.Columns[1].HeaderText = "Author Lastname";
                        GridView_base.Columns[2].HeaderText = "Title of the book";
                    }
                    else
                    {
                        MessageBox.Show("No records found", "No data", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        GridView_base.DataSource = null;
                    }
                }
            }
        }
예제 #10
0
 private void ManageAuthors_Load(object sender, EventArgs e)
 {
     using (var db = new LAM_Lab2Context())
     {
         //Hides two columns from the dataGridView
         dataGridView_Authors.DataSource = db.Authors.ToList();
         this.dataGridView_Authors.Columns["AFirstName"].Visible = false;
         this.dataGridView_Authors.Columns["ALastName"].Visible  = false;
     }
 }
예제 #11
0
        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);
                }
            }
        }
예제 #12
0
        private void dataGridView_books_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            using (var db = new LAM_Lab2Context())
            {
                //Updates the textboxes and the other controls with the values from dataGridView,
                //and enables the Update-button.
                var id   = dataGridView_books.Rows[e.RowIndex].Cells[0].Value.ToString();
                var book = db.Books.Find(id);

                tb_ISBN.Text                = book.Isbn13;
                tb_title.Text               = book.Title;
                tb_Price.Text               = book.Price.ToString();
                tb_Language.Text            = book.Language;
                checkBox_activeBook.Checked = book.Active;

                this.button_saveUpdateBook.Enabled = true;
            }
        }
예제 #13
0
        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);
                }
            }
        }
예제 #14
0
 public object sqlResult()
 {
     //LINQ query for the dataGridView.
     using (var db = new LAM_Lab2Context())
     {
         var storedata2 = from stockbalance in db.StockBalances
                          join book in db.Books
                          on stockbalance.BooksIsbn13 equals book.Isbn13
                          join store in db.BookStores
                          on stockbalance.BookStoresId equals store.Id
                          where book.Active == true
                          select new
         {
             store.Name,
             book.Title,
             stockbalance.NumberOfItems
         };
         var storedata = storedata2.ToList();
         return(storedata);
     }
 }
예제 #15
0
        private void Form1_Load(object sender, EventArgs e)
        {
            using (var db = new LAM_Lab2Context())
            {
                //Show everything in the Books-table in the dataGridView.
                //If empty, show message.
                var booksData = from b in db.Books select b;

                if (booksData != null)
                {
                    if (booksData.Count() > 0)
                    {
                        dataGridView_books.DataSource = booksData.ToList();
                        this.dataGridView_books.Columns["OrderDetails"].Visible = false;
                    }
                    else
                    {
                        MessageBox.Show("No records found", "No data", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        dataGridView_books.DataSource = null;
                    }
                }
            }
        }
예제 #16
0
        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);
                }
            }
        }