示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            string found = _context.Customers
                           .Where(c => c.CompanyName.Equals(this.textBox4.Text))
                           .Select(c => c.CompanyName)
                           .FirstOrDefault <string>();

            if (found == null)
            {
                MessageBox.Show("No such customer exits");
                return;
            }

            if (this.checkedListBox1.CheckedItems.Count < 1)
            {
                MessageBox.Show("Please add some products to the order");
                return;
            }

            List <int> selectedProductsIds = new List <int>();

            foreach (var selected in this.checkedListBox1.CheckedItems)
            {
                String[] tokens = selected.ToString().Split('-');
                selectedProductsIds.Add(Int32.Parse(tokens[1]));
            }

            Order newOrder = new Order
            {
                Notes           = this.richTextBox1.Text,
                OrderedDateTime = DateTime.Now,
                CustomerId      = found,
            };

            _context.Orders.Add(newOrder);
            foreach (var prod in this.products.Where(p => selectedProductsIds.Contains(p.ProductId)))
            {
                prod.InOrders.Add(newOrder);
            }
            _context.SaveChanges();

            _context.Dispose();
            Hide();
            DestroyHandle();
        }
示例#2
0
        // Save and close
        private void button1_Click(object sender, EventArgs e)
        {
            int belongsToCategory = 0;

            using (var context = new ProdContext())
            {
                belongsToCategory = context.Categories
                                    .Where(c => c.Name == (string)comboBox1.SelectedItem)
                                    .Select(c => c.CategoryId)
                                    .FirstOrDefault <int>();
            }
            if (belongsToCategory == 0)
            {
                MessageBox.Show("Please select a category");
                return;
            }
            if (textBox1.Text == "")
            {
                MessageBox.Show("Please provide a name");
                return;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("Please provide the unit price");
                return;
            }
            if (textBox3.Text == "")
            {
                MessageBox.Show("Please provide the number of units in stock");
                return;
            }
            var newProduct = new Product
            {
                CategoryId = belongsToCategory,
                Name       = textBox1.Text
            };

            try
            {
                newProduct.UnitPrice = Decimal.Parse(textBox2.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Unit price must be a decimal value");
                return;
            }
            try
            {
                newProduct.UnitsInStock = Int32.Parse(textBox3.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("The number of units in stock must be an integer");
                return;
            }
            using (var context = new ProdContext())
            {
                context.Products.Add(newProduct);
                context.SaveChanges();
            }
            Hide();
            DestroyHandle();
        }