コード例 #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                bool NameNull     = string.IsNullOrEmpty(textboxBookName.Text);
                bool YearNull     = string.IsNullOrEmpty(textboxYear.Text);
                bool CompanyNull  = string.IsNullOrEmpty(textboxCompany.Text);
                bool CategoryNull = string.IsNullOrEmpty(cbbCategory.SelectedItem.ToString());
                bool AuthorsNull  = (listTacGia == null);

                if (!NameNull && !YearNull && !CompanyNull && !CategoryNull && !AuthorsNull)
                {
                    string name    = textboxBookName.Text;
                    int    year    = Int32.Parse(textboxYear.Text);
                    string company = textboxCompany.Text;

                    //get category
                    string category   = cbbCategory.SelectedItem.ToString();
                    string categoryID = CategoryController.GetCategoryIDByName(category);

                    //get authors
                    List <string> authorsID = new List <string>();
                    foreach (Button author in listTacGia.Controls)
                    {
                        authorsID.Add(AuthorController.GetAuthorIDByName(author.Text));
                    }

                    if (BookController.AddBook(name, categoryID, authorsID, company, year))
                    {
                        MessageBox.Show("Them thanh cong");
                        Clear();
                        this.Hide();
                        this.isShown = false;
                        if (parent != null)
                        {
                            this.parent.ClearAndShow();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Thêm thất bại, vui lòng kiểm tra lại thông tin");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Thông tin không hợp lệ");
            }
        }
コード例 #2
0
        //button Update
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                bool NameNull     = string.IsNullOrEmpty(textboxName.Text);
                bool YearNull     = string.IsNullOrEmpty(textboxYear.Text);
                bool CompanyNull  = string.IsNullOrEmpty(textboxCompany.Text);
                bool CategoryNull = string.IsNullOrEmpty(cbbCategory.SelectedItem.ToString());
                bool AuthorsNull  = (listTacGia == null);

                if (!NameNull && !YearNull && !CompanyNull && !CategoryNull && !AuthorsNull)
                {
                    string id      = labelID.Text;
                    string name    = textboxName.Text;
                    int    year    = Int32.Parse(textboxYear.Text);
                    string company = textboxCompany.Text;

                    //get category
                    string category   = cbbCategory.SelectedItem.ToString();
                    string categoryID = CategoryController.GetCategoryIDByName(category);

                    //get authors
                    List <string> authorsID = new List <string>();
                    foreach (Button author in listTacGia.Controls)
                    {
                        authorsID.Add(AuthorController.GetAuthorIDByName(author.Text));
                    }

                    if (BookController.UpdateBook(id, name, categoryID, authorsID, company, year))
                    {
                        MessageBox.Show("Update thanh cong");
                        this.Hide();
                        Clear();
                    }
                    else
                    {
                        MessageBox.Show("Update that bai");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Thong tin khong hop le");
            }
        }