コード例 #1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            #region Data validation
            //data validation
            if (this.txtBookName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Must have the BookName");
                this.txtBookName.Focus();
                return;
            }

            if (this.cboBookCategory.SelectedIndex == -1)
            {
                MessageBox.Show("Select the Book Category");
                return;
            }
            if (this.pbCurrentImage.Image == null)
            {
                MessageBox.Show("Select an image for book");
                return;
            }
            if (this.cboPublisher.SelectedIndex == -1)
            {
                MessageBox.Show("Select the Publisher for the book");
                return;
            }
            if (this.txtAuthor.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please input the Author for the book");
                this.txtAuthor.Focus();
                return;
            }
            if (this.txtUnitPrice.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please input the Book price");
                this.txtUnitPrice.Focus();
                return;
            }

            if (this.txtBarCode.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please entry the number which is under the Barcode");
                this.txtBarCode.Focus();
                return;
            }



            #endregion

            //instance of object
            Books objBook = new Books()
            {
                BookName       = this.txtBookName.Text.Trim(),
                BookCategoryId = Convert.ToInt32(this.cboBookCategory.SelectedValue),
                PublisherId    = Convert.ToInt32(this.cboPublisher.SelectedValue),
                PublisherDate  = Convert.ToDateTime(this.dtpPublishDate.Text),
                Author         = this.txtAuthor.Text.Trim(),
                UnitPrice      = Convert.ToInt32(this.txtUnitPrice.Text.Trim()),
                BarCode        = this.txtBarCode.Text.Trim(),
                BookCount      = Convert.ToInt32(this.txtBookCount.Text.Trim()),
                Remainder      = Convert.ToInt32(this.txtBookCount.Text.Trim()),
                BookPosition   = this.txtBookPosition.Text.Trim(),
                BookImage      = new Common.SerializeObjectToString().SerializeObject(this.pbCurrentImage.Image),
                PublisherName  = this.cboPublisher.Text
            };


            //call B-E
            try
            {
                objBookManager.AddBookk(objBook);
                this.booklist.Add(objBook);
                this.dgvBookList.DataSource = null;
                this.dgvBookList.DataSource = this.booklist;
                //clearn the book infomation
                foreach (Control item in this.gbBook.Controls)
                {
                    if (item is TextBox)
                    {
                        item.Text = "";
                    }
                    else if (item is ComboBox)
                    {
                        ((ComboBox)item).SelectedIndex = -1;
                    }
                }
                this.pbCurrentImage.Image = null;
                this.txtBookName.Focus();
            }
            catch (Exception ex)
            {
                throw;
            }
        }