Ejemplo n.º 1
0
        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            ProductModel product = new ProductModel();

            product.ProductID = int.Parse(txtProductID.Text);

            try
            {
                //Textbox Name
                if (txtName.Text.Trim() == "")
                {
                    MessageBox.Show("Please fill Name", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    product.Name = txtName.Text;
                }

                //Textbox Shotcut Name
                product.ShortName = txtSCName.Text.Trim() == "" ? "" : txtSCName.Text;

                //Textbox Desicription
                product.Description = txtDescription.Text.Trim() == "" ? "" : txtDescription.Text;

                //Textbox Popup
                if (cbPopup.Text == "")
                {
                    product.PopupID = 0;
                }
                else
                {
                    product.PopupID = int.Parse(cbPopup.SelectedValue.ToString());
                }

                //Textbox PriceInc
                if (txtPriceInc.Text.Trim() == "")
                {
                    product.Price = 0;
                }
                else
                {
                    float parsedValue;
                    if (!float.TryParse(txtPriceInc.Text, out parsedValue))
                    {
                        MessageBox.Show("Price field is only number!");
                        txtPriceInc.Text = "0";
                        return;
                    }
                    else
                    {
                        product.Price = parsedValue;
                    }
                }

                //Textbox Stock
                if (txtStock.Text == "")
                {
                    product.Stock = 0;
                }
                else
                {
                    int parsedValue;
                    if (!int.TryParse(txtPriceInc.Text, out parsedValue))
                    {
                        MessageBox.Show("Stock field is only number!");
                        txtStock.Text = "";
                        return;
                    }
                    else
                    {
                        product.Price = parsedValue;
                    }
                }

                //Checkbox Avaliable
                product.Avaliable = cbAvaliable.Checked == true ? 1 : 0;

                //Type of Food
                if (rbEntree.Checked == true)
                {
                    product.TypeOfFood = 1;
                }
                else if (rbMain.Checked == true)
                {
                    product.TypeOfFood = 2;
                }
                else if (rbBeverage.Checked == true)
                {
                    product.TypeOfFood = 3;
                }
                else if (rbDessert.Checked == true)
                {
                    product.TypeOfFood = 4;
                }
                else
                {
                    product.TypeOfFood = 5;
                }

                string json = JsonConvert.SerializeObject(product);

                DatabaseHandle dbHandle = new DatabaseHandle();

                if (dbHandle.AddProduct(json) > 0)
                {
                    MessageBox.Show("Add Product Complete!");
                }
                else
                {
                    MessageBox.Show("Something Wrong!");
                }

                dbHandle = null;
                ResetForm();
            }
            catch (Exception ex)
            {
                ExceptionLog exceptionLog = new ExceptionLog();
                exceptionLog.Message = ex.Message;
            }
        }