public string Save(Book aBook )
 {
     if (bookGateway.HasBookName(aBook.Title))
     {
         throw new Exception("This system has this book name.Please try again.");
     }else if (bookGateway.HasISBNNo(aBook.ISbnNo))
     {
         throw new Exception("This system already have this ISBN No.Please try again.");
     }
     else
     {
         return bookGateway.Save(aBook);
     }
 }
 private void saveButton_Click(object sender, EventArgs e)
 {
     Book aBook=new Book();
     if (booknameTextBox.Text == string.Empty)
     {
         MessageBox.Show(@"Please entre book title.","Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     else if (authorNameTextBox.Text == string.Empty)
     {
         MessageBox.Show("Please entre the author name.", "Information", MessageBoxButtons.OK,
                         MessageBoxIcon.Information);
     }
     else if (iSBNNoTextBox.Text==string.Empty)
     {
         MessageBox.Show("Please entre ISBN no.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     else if (priceTextBox.Text==string.Empty)
     {
         MessageBox.Show("Please entre the prie of this book.", "Information", MessageBoxButtons.OK,
                         MessageBoxIcon.Information);
     }
     else if (noOfInitiatCopyTextBox.Text==string.Empty)
     {
         MessageBox.Show("Give how many book in your store.", "Information", MessageBoxButtons.OK,
                         MessageBoxIcon.Information);
     }
     else
     {
         string message = null;
         try
         {
             aBook.Id = Convert.ToInt32(bookIdTextBox.Text);
             aBook.Title = booknameTextBox.Text;
             aBook.AuthorName = authorNameTextBox.Text;
             aBook.ISbnNo = iSBNNoTextBox.Text;
             aBook.Price = Convert.ToInt32(priceTextBox.Text);
             aBook.InitialCopy = Convert.ToInt32(noOfInitiatCopyTextBox.Text);
             aBook.OutsideCopy = 0;
             message = bookManager.Save(aBook);
             MessageBox.Show(message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
             ClearAll();
         }
         catch (Exception exception)
         {
             MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }
        public Book SearchBook(int bookId)
        {
            Book aBook = new Book();
            try
            {
                SqlConnectionObj.Open();
                string query = string.Format("select * from tbl_book where Id='{0}'", bookId);
                SqlCommandObj.CommandText = query;
                SqlDataReader reader = SqlCommandObj.ExecuteReader();
                while (reader.Read())
                {

                    aBook.Id = Convert.ToInt32(reader["Id"]);
                    aBook.Title = reader["Title"].ToString();
                    aBook.AuthorName = reader["AuthorName"].ToString();
                    aBook.ISbnNo = reader["IsbnNo"].ToString();
                    aBook.InitialCopy = Convert.ToInt32(reader["InitialCopy"]);
                    aBook.OutsideCopy = Convert.ToInt32(reader["OutsideCopy"]);
                    aBook.Price = Convert.ToInt32(reader["Price"]);
                }
            }
            catch (Exception exception)
            {

                throw new Exception("Error occurred during search this book.",exception);
            }
            finally
            {
                SqlConnectionObj.Close();
            }
            return aBook;
        }
        public string Save(Book aBook)
        {
            try
            {
                SqlConnectionObj.Open();
                string query = string.Format("INSERT INTO tbl_book VALUES({0},'{1}','{2}','{3}',{4},{5},{6})",aBook.Id, aBook.Title, aBook.AuthorName, aBook.ISbnNo, aBook.Price, aBook.InitialCopy,aBook.OutsideCopy);
                SqlCommandObj.CommandText = query;
                SqlCommandObj.ExecuteNonQuery();
                return "Book have been saved.";
            }
            catch (Exception ex)
            {

                throw new Exception("Book couldn't be saved.",ex);
            }finally
            {
                SqlConnectionObj.Close();
            }
        }
        public List<Book> GetAllBooks(string name, string searchType)
        {
            List<Book> books=new List<Book>();
            try
            {
                SqlConnectionObj.Open();
                if (searchType == "name")
                {
                    string quere = string.Format("select * from tbl_book where Title like '%{0}%'", name );
                    SqlCommandObj.CommandText = quere;
                    SqlDataReader reader = SqlCommandObj.ExecuteReader();

                    while (reader.Read())
                    {
                        Book aBook = new Book();
                        aBook.Id = Convert.ToInt32(reader["Id"]);
                        aBook.Title = reader["Title"].ToString();
                        aBook.AuthorName = reader["AuthorName"].ToString();
                        aBook.InitialCopy = Convert.ToInt32(reader["InitialCopy"]);
                        aBook.OutsideCopy = Convert.ToInt32(reader["OutsideCopy"]);
                        aBook.Price = Convert.ToInt32(reader["Price"]);
                        books.Add(aBook);
                    }

                }
                else if (searchType == "author")
                {
                    string quere = string.Format("select * from tbl_book where AuthorName like '%{0}%'", name);
                    SqlCommandObj.CommandText = quere;
                    SqlDataReader reader = SqlCommandObj.ExecuteReader();

                    while (reader.Read())
                    {
                        Book aBook = new Book();
                        aBook.Id = Convert.ToInt32(reader["Id"]);
                        aBook.Title = reader["Title"].ToString();
                        aBook.AuthorName = reader["AuthorName"].ToString();
                        aBook.InitialCopy = Convert.ToInt32(reader["InitialCopy"]);
                        aBook.OutsideCopy = Convert.ToInt32(reader["OutsideCopy"]);
                        aBook.Price = Convert.ToInt32(reader["Price"]);
                        books.Add(aBook);
                    }
                }
                else if (searchType == "bookId")
                {
                    string quere = string.Format("select * from tbl_book where Id={0}", name);
                    SqlCommandObj.CommandText = quere;
                    SqlDataReader reader = SqlCommandObj.ExecuteReader();

                    while (reader.Read())
                    {
                        Book aBook = new Book();
                        aBook.Id = Convert.ToInt32(reader["Id"]);
                        aBook.Title = reader["Title"].ToString();
                        aBook.AuthorName = reader["AuthorName"].ToString();
                        aBook.InitialCopy = Convert.ToInt32(reader["InitialCopy"]);
                        aBook.OutsideCopy = Convert.ToInt32(reader["OutsideCopy"]);
                        aBook.Price = Convert.ToInt32(reader["Price"]);
                        books.Add(aBook);
                    }
                }
            }
            catch (Exception exception)
            {

                throw new Exception("Error occurred during books loading from your system.", exception);
            }finally
            {
                SqlConnectionObj.Close();
            }
            return books;
        }
        public Book GetBooks(int toInt32)
        {
            try
            {
                SqlConnectionObj.Open();
                string query = string.Format("select * from tbl_book where Id={0}", toInt32);
                SqlCommandObj.CommandText = query;
                SqlDataReader reader = SqlCommandObj.ExecuteReader();
                Book aBook=new Book();
                while (reader.Read())
                {
                    aBook.Id = Convert.ToInt32(reader[0]);
                    aBook.Title = reader[1].ToString();
                    aBook.AuthorName = reader[2].ToString();
                    aBook.ISbnNo = reader[3].ToString();
                    aBook.Price = Convert.ToInt32(reader[4]);
                }
                return aBook;
            }
            catch (Exception exception)
            {

                throw new Exception("System can not return those books.",exception);
            }
            finally
            {
                SqlConnectionObj.Close();
            }
        }
        private void addButton_Click(object sender, EventArgs e)
        {
            addIssueBooksDataGridView.DataSource = null;
            int i = 0;
             Book aBook = new Book();
             aBook.Id = Convert.ToInt32(bookSearchtextBox.Text);
             aBook.Title = bookNameTextBox.Text;
             aBook.AuthorName = bookAuthorTextBox.Text;
             aBook.OutsideCopy = Convert.ToInt32(bookLendTextBox.Text);

             if (books.Count<1)
            {
                if (Convert.ToInt32(bookAvailableTextBox.Text) - Convert.ToInt32(bookLendTextBox.Text) >= 0)
                {
                    if(Convert.ToInt32(bookLendTextBox.Text) !=0)
                    {
                        books.Add(aBook);
                        LoadDatagridview(books);
                    }
                    else
                    {
                        MessageBox.Show("Please insert how many books you want.");
                    }
                }
                else
                {
                    MessageBox.Show("");
                }
            }
               else
            {
                foreach (Book book in books)
                {
                    if (book.Id==aBook.Id)
                    {
                        i = 1;
                        int noOfBook = book.OutsideCopy + aBook.OutsideCopy;

                        if (Convert.ToInt32(bookAvailableTextBox.Text)-noOfBook>=0)
                        {
                            book.OutsideCopy += aBook.OutsideCopy;
                            goto solution;
                        }
                        else
                        {
                            MessageBox.Show("No available book to lend");
                        }
                    }
                }
             solution:
                 if (i == 0)
                 {
                     if (Convert.ToInt32(bookAvailableTextBox.Text) - aBook.OutsideCopy >= 0)
                     {
                         books.Add(aBook);
                         LoadDatagridview(books);
                     }
                     else
                     {
                         MessageBox.Show("No available book to lend");
                         LoadDatagridview(books);
                     }
                 }
                 else
                 {
                     LoadDatagridview(books);
                 }
            }
        }
 private void bookSerachButton_Click(object sender, EventArgs e)
 {
     if (bookSearchtextBox.Text!="")
     {
         try
         {
             bookId = Convert.ToInt32(bookSearchtextBox.Text);
             Book book = new Book();
             book = bookManager.SearchBook(bookId);
             bookNameTextBox.Text = book.Title;
             bookAuthorTextBox.Text = book.AuthorName;
             bookAvailableTextBox.Text = book.InitialCopy.ToString();
             bookLendTextBox.Text = "0";
             if (book.InitialCopy<1)
             {
                 bookLendTextBox.ReadOnly = true;
             }
             else
             {
                 addButton.Enabled = true;
                 groupBox2.Enabled = true;
             }
         }
         catch (Exception exception)
         {
             MessageBox.Show(exception.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
     else
     {
         MessageBox.Show("Please Fill the book ID text box.");
     }
 }