コード例 #1
0
        private async void BtnAddProduct_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtboxaddname.Text) ||
                lstboxAddtype.SelectedIndex <= 0)
            {
                return;
            }

            int price = 0;

            if (!int.TryParse(txtboxaddprice.Text, out price))
            {
                MessageBox.Show("Price must be a number");
                return;
            }

            var newProduct = new Product()
            {
                ProductTypeID = (ProductType)lstboxAddtype.SelectedIndex,
                ProductName   = txtboxaddname.Text,
                BasePrice     = price,
                Description   = txtboxadddescr.Text
            };

            try
            {
                await _repo.AddNewProductAsync(newProduct);

                MessageBox.Show("Product: " + newProduct.ProductName + " was added!");
            }
            catch
            {
                MessageBox.Show("ERROR. Could not import product - Try Again.");
            }

            txtboxaddname.Text  = "";
            txtboxaddprice.Text = "";
            txtboxadddescr.Text = "";
        }