private async void addPurchaseButton_Click(object sender, RoutedEventArgs e) { try { //Customer var dt = QueryForSQLServer.GetCustomerByTel(customerTelTextBox.Text.ToString()); if (dt.Rows.Count == 0) { var _newCustomer = new Customer { Tel = customerTelTextBox.Text.ToString(), Customer_Name = customerNameTextBox.Text.ToString() }; QueryForSQLServer.InsertCustomer(_newCustomer); } else { customerNameTextBox.Text = dt.Rows[0].ItemArray[0].ToString(); } //Purchase var purchase = new Purchase() { Created_At = DateTime.Now, Total = list.Sum((dynamic p) => p.SubTotal as Nullable <decimal>), Customer_Tel = customerTelTextBox.Text.ToString(), Status = (PurchaseStatus)(purchaseStatesComboBox.SelectedIndex + 1) }; var p_id = QueryForSQLServer.InsertPurchase(purchase); //PurchaseDetail if (p_id == -1) { var messageDialog = await new MessageDialog("Failed", "Confirm").ShowAsync(); return; } else { foreach (dynamic item in list) { purchase.PurchaseDetails.Add(new PurchaseDetail() { Purchase_ID = p_id, Product_ID = item.Product_ID, Price = item.Unit_Price, Quantity = item.Quantity, Total = item.SubTotal }); } foreach (dynamic item in purchase.PurchaseDetails) { QueryForSQLServer.InsertPurchaseDetail(item); } } var messageDialog2 = await new MessageDialog("Success", "Confirm").ShowAsync(); } catch (Exception ex) { Debug.WriteLine(ex); } }