private void EnterButton_Click(object sender, RoutedEventArgs e) { Product product = (Product)ProductsDataGrid.SelectedItem; bool unique = true; var lastOrder = myDbContext.Orders.ToList().Last(); // последняя запись в таблице заказов // проверяем, не пытается ли пользователь добавить один и тот же товар дважды foreach (OrderProduct orderProduct in myDbContext.OrderProducts) { if ((orderProduct.OrderId == lastOrder.OrderId) && (orderProduct.Product.ProductName == product.ProductName)) { unique = false; } } if (unique == true) { try { if (SupplierNamesComboBox.Text != "") { int orderId = lastOrder.OrderId; int productId = product.ProductId; int amount = Convert.ToInt32(AmountTextBox.Text); double price = product.Price * 0.3; // закупочная цена = розничная цена - 30% lastOrder.SupplierId = Supplier.GetSupplierBySupplierName(SupplierNamesComboBox.Text).SupplierId; // определяем поставщика OrderProduct.Insert(orderId, productId, amount, price); // добавляем товар в заказ myDbContext.SaveChanges(); MessageBoxResult result = MessageBox.Show($"Товар успешно добавлен. Хотите еще добавить товар?", "Сообщение", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { } else { Close(); } } else { MessageBox.Show("Выберите поставщика из списка.", "Ошибка"); } } catch { MessageBox.Show("Проверьте корректность введенных данных!", "Ошибка"); } } else { MessageBox.Show("Данный товар уже имеется в текущем заказе!", "Ошибка"); } }
private void Button_Save(object sender, EventArgs e) { if (!CheckGood()) { MessageBox.Show("You didn't write right,\n You need to: \n " + to_change, "TRY AGAIN", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); All_White(); to_change = ""; } else { OrderProduct orderProduct = FormToOrderProduct(); if (orderProduct.ID == 0) { if (orderProduct.Insert()) { MessageBox.Show("Order Details Saved"); Clean_Form(); OrderProductArrToForm(); } else { MessageBox.Show("Cannot Save Order Details"); } } else { if (orderProduct.Update()) { MessageBox.Show("Order Details UPDATED"); Clean_Form(); OrderProductArrToForm(); } else { MessageBox.Show("Cannot UPDATE order Details"); } } } }