コード例 #1
0
ファイル: ProductManagement.cs プロジェクト: huynhp0502/.NET
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbStatusCate.Text.Equals("Deleted"))
                {
                    MessageBox.Show("This Category is unusable");
                }
                else
                {
                    if (txtCategoryID.TextLength != 0 && txtName.TextLength != 0 && txtPrice.TextLength != 0 &&
                        txtProductID.TextLength != 0 && txtQuantity.TextLength != 0)
                    {
                        double f;
                        int    i;
                        if (double.TryParse(txtPrice.Text, out f) && int.TryParse(txtQuantity.Text, out i))
                        {
                            int    quantity = Int32.Parse(txtQuantity.Text);
                            double price    = Double.Parse(txtPrice.Text);
                            bool   chkNum   = true;
                            if (quantity < 0)
                            {
                                chkNum = false;
                                MessageBox.Show("Quantity must be positive");
                                return;
                            }
                            if (price <= 0)
                            {
                                chkNum = false;
                                MessageBox.Show("Price must be bigger than 0");
                                return;
                            }
                            if (chkNum)
                            {
                                string cateId  = txtCategoryID.Text;
                                string proID   = txtProductID.Text;
                                string proName = txtName.Text;
                                if (productData.AddNewProduct(new Product(cateId, proID, proName, price, quantity)))
                                {
                                    dtProduct.RejectChanges();

                                    dtProduct.Rows.Add(proID, proName, quantity, price);

                                    dtProduct.AcceptChanges();
                                    MessageBox.Show("New Product added!");
                                    lbStatusProduct.Text = "";
                                    LoadProduct();
                                }
                                else
                                {
                                    MessageBox.Show("Error when adding product !");
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("Quantity and Price must be number!!");
                        }
                    }
                    else
                    {
                        MessageBox.Show("You left some textfield blank!!! ");
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("duplicate"))
                {
                    MessageBox.Show("This productID is already existed");
                }
                else
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }