private void tsrAdd_Click(object sender, EventArgs e) { lblErrorInfo.Text = ""; AddUpdateForm addForm = new AddUpdateForm(); addForm.ShowDialog(); if (addForm.Order != null && addForm.Mode == 0)//Means create; { try { if (addForm.Order.ID == -1) { orderService.AddOrder(addForm.Order.Customer); orderService.UpdateItems(orderService.Orders[orderService.Orders.Count - 1].ID, addForm.Order.OrderItems); } else { orderService.AddOrder(addForm.Order); } } catch (OrderException x) { switch (x.Code) { case 3: lblErrorInfo.Text = "Invalid ID : ID < 0."; break; case 6: lblErrorInfo.Text = "Invalid ID : This ID has already existed."; break; default: break; } } } else if (addForm.Order != null && addForm.Mode == 1)//Means update. { try { orderService.UpdateItems(addForm.Order.ID, addForm.Order.OrderItems); } catch (OrderException x) { if (x.Code == 6) { lblErrorInfo.Text = "No such target order, id is urgently required."; } else { lblErrorInfo.Text = "Items are null, meaningless."; } } } addForm.Dispose(); }
private void tsrAdd_Click(object sender, EventArgs e) { lblErrorInfo.Text = ""; AddUpdateForm addForm = new AddUpdateForm(); addForm.ShowDialog(); if (addForm.Order != null && addForm.Mode == 0)//Means create; { try { if (addForm.Order.OrderId == -1) { int newId = orderService.AddOrder(addForm.Order.Customer); if (addForm.OrderItem != null) { List <OrderItem> orderItems = new List <OrderItem>(); orderItems.Add(addForm.OrderItem); orderService.UpdateItems(newId, orderItems); } } else { using (var context = new OrderContext()) { var exist = context.Customers.FirstOrDefault(x => x.Name == addForm.Order.Customer.Name); if (exist != null) { addForm.Order.CustomerName = exist.Name; } } orderService.AddOrder(addForm.Order); if (addForm.OrderItem != null) { List <OrderItem> orderItems = new List <OrderItem>(); orderItems.Add(addForm.OrderItem); orderService.UpdateItems(addForm.Order.OrderId, orderItems); } } } catch (OrderException x) { switch (x.Code) { case 3: lblErrorInfo.Text = "Invalid ID : ID < 0."; break; case 6: lblErrorInfo.Text = "Invalid ID : This ID has already existed."; break; default: break; } } } else if (addForm.Order != null && addForm.Mode == 1)//Means update. { try { List <OrderItem> orderItems = new List <OrderItem>(); orderItems.Add(addForm.OrderItem); orderService.UpdateItems(addForm.Order.OrderId, orderItems); } catch (OrderException x) { if (x.Code == 6) { lblErrorInfo.Text = "No such target order, id is urgently required."; } else { lblErrorInfo.Text = "Items are null, meaningless."; } } } addForm.Dispose(); }