public EditOrderDialog(OrderFullDetails order) { InitializeComponent(); this.order = order; // Fill the form with existing information this.startDateEditOrder.SelectedDate = order.startDate; this.endDateEditOrder.SelectedDate = order.endDate; this.drivingLicenseEditOrder.Text = order.license; this.carListEditOrder.SelectedValue = order.regNumber; LoadCarComboBox(); LoadCustomerDetails(); }
// EDIT/DELETE order private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (listView.SelectedItems.Count > 0) { var item = (dynamic)listView.SelectedItems[0]; OrderFullDetails order = new OrderFullDetails { ID = item.ID, startDate = item.startDate, endDate = item.endDate, duration = item.duration, customer = item.customer, license = item.license, regNumber = item.regNumber, dailyRate = item.dailyRate, total = item.total }; EditOrderDialog dialog = new EditOrderDialog(order); bool? result = dialog.ShowDialog(); if (result == true) { // Getting data from a dialog form DateTime fromDate = (DateTime)dialog.startDateEditOrder.SelectedDate; DateTime toDate = (DateTime)dialog.endDateEditOrder.SelectedDate; // Calc the duration int duration = (toDate - fromDate).Days; // >>> carID is actually a regNumber HERE!!! String carID = dialog.carListEditOrder.Text; String custName = dialog.firstnameEditOrder.Text; String custLastName = dialog.lastnameEditOrder.Text; String drivingLicense = dialog.drivingLicenseEditOrder.Text; if (fromDate != null && toDate != null && carID != null && custName != null && custLastName != null && drivingLicense != null) { // Initialize PKs for new order insertion // Getting a PK of a car using the regNumber (also unique) int carPK = (from c in this.dbContext.Cars where c.regNumber == carID select c.ID).FirstOrDefault(); // Set to 0, reassign value later in the code depending on condition int customerPK = 0; Boolean newCustomer = true; // Checking if a customer already in DB var query = from cu in this.dbContext.Customers select cu; foreach (var customer in query) { if (drivingLicense.ToUpper() == customer.drivingLicense.ToUpper()) { // Customer in a DB, taking his ID customerPK = customer.ID; newCustomer = false; break; } } // If customer is new if (newCustomer) { // Save customer to DB Customers newCu = new Customers { firstName = custName, lastName = custLastName, drivingLicense = drivingLicense }; this.dbContext.Customers.Add(newCu); this.dbContext.SaveChanges(); // Fetch an just inserted customer's ID customerPK = newCu.ID; } int oUpdateID = item.ID; var queryUpdate = from o in this.dbContext.Orders where o.ID == oUpdateID select o; foreach (Orders o in queryUpdate) { o.carID = carPK; o.customerID = customerPK; o.startDate = fromDate; o.duration = duration; } this.dbContext.SaveChanges(); } } // Refresh the list switch (orderListType) { case 0: DisplayAllOrders(); break; case 1: DisplayCurrentOrders(); break; case 2: DisplayPendingOrders(); break; case 3: DisplayCompletedOrders(); break; } } }