private void SaveButtonClick(object sender, RoutedEventArgs e) { Contract contract = new Contract(); contract.ContractNo = int.Parse(TextBoxContractNo.Text); contract.CreationDate = DateTime.Now; contract.Dealer = dealers[ComboBoxDealer.SelectedIndex]; contract.Customer = customers[ComboBoxCustomer.SelectedIndex]; contract.ReturnDate = ReturnDate.SelectedDate.Value; int price; if (int.TryParse(Advance.Text, out price)) contract.Advance = price; else { MessageBox.Show("Advance price error"); return; } if (int.TryParse(Total.Text, out price)) contract.TotalPrice = price; else { MessageBox.Show("Total price error"); return; } foreach (StackPanel item in ToolsGrid.Items) { if (item.DataContext == null) continue; Machine rentedMachine = item.DataContext as Machine; contract.Machines.Add(rentedMachine); engine.MachineRepository.Update(rentedMachine); } foreach (StackPanel item in AccessoryGrid.Items) { int count; if (item.DataContext == null || int.TryParse((item.Children[1] as TextBox).Text, out count)) continue; Accessory rentedAccesory = new Accessory(); rentedAccesory.Name = (item.Children[0] as TextBlock).Text; rentedAccesory.Count = count; } contract.GetPromoted(); engine.ContractRepository.Insert(contract); UpdateContracts(); contract = contracts[contracts.Count - 1]; int lastContractId = engine.ContractRepository.Get(null, q => q.OrderBy(s => s.ContractNo), string.Empty).Last().ContractId; foreach (Machine item in contract.Machines) item.ContractId = lastContractId; UpdateMachines(); WindowHelpers.ClearMainWindow(this,false); TextBoxContractNo.Text = (contract.ContractNo + 1).ToString(); }