Пример #1
0
        private void AddItemButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Name.Text) && !string.IsNullOrEmpty(Price.Text) && !string.IsNullOrEmpty(Stock.Text))
            {
                int     IntTest;
                Double  DoubleTest;
                Boolean Fail = true;
                while (Fail)
                {
                    if (Double.TryParse(Price.Text, out DoubleTest))
                    {
                        _Product.Price = Double.Parse(Price.Text);
                    }
                    else
                    {
                        MessageBox.Show("Not an acceptable price");
                        Fail = false;
                        break;
                    }

                    if (Int32.TryParse(Stock.Text, out IntTest))
                    {
                        _Product.Stock_Level = Int32.Parse(Stock.Text);
                    }
                    else
                    {
                        MessageBox.Show("Not an acceptable stock level");
                        Fail = false;
                        break;
                    }

                    _Product.Product_Name = Name.Text;
                    _Product.ProductId    = _PHPRepo.GetMaxProductId() + 1;
                    _PHPRepo.AddProductRecord(_Product);

                    MessageBox.Show("New product added successfully.");

                    StockList.Items.Clear();
                    List <Product> _NewList = _PHPRepo.GetProducts();
                    foreach (Product p in _NewList)
                    {
                        string[] row          = { p.ProductId.ToString(), p.Product_Name.ToString(), p.Price.ToString(), p.Stock_Level.ToString() };
                        var      listViewItem = new ListViewItem(row);
                        StockList.Items.Add(listViewItem);
                    }
                    ;

                    Name.Clear();
                    Price.Clear();
                    Stock.Clear();

                    Fail = false;
                    break;
                }
            }
            else
            {
                MessageBox.Show("Please fill out all fields");
            }
        }