//make transaction private void MakeTransaction() { Transaction transac = null; Transaction depositToBenefAccount = null; try { decimal amount = decimal.Parse(tbAmount.Text); transac = new Transaction(); transac.Date = DateTime.Today; transac.Amount = amount; transac.Type = currentTransType; transac.AccountId = currentAccount.Id; if (currentTransType == "Transfer") { int destinationAccNo = int.Parse(tbBenefAccNo.Text); transac.ToAccount = destinationAccNo; } if (currentTransType == "Payment") { User payee = (User)comboPayees.SelectedItem; transac.ToAccount = (from a in EFData.context.Accounts where a.UserId == payee.Id && a.AccountType.Id == 4 select a.Id).FirstOrDefault(); transac.PaymentCategory = comboPayCategory.Text; } EFData.context.Transactions.Add(transac); decimal previousBalance = currentAccount.Balance; //balance before transaction currentAccount.Balance = DeductAddMoneyToAccount(currentTransType, previousBalance, amount); //new balance if (currentTransType == "Transfer" || currentTransType == "Payment") { Account beneficiaryAcc = EFData.context.Accounts.SingleOrDefault(a => a.Id == transac.ToAccount); depositToBenefAccount = new Transaction { Date = DateTime.Today, Amount = amount, Type = "Deposit", AccountId = beneficiaryAcc.Id }; EFData.context.Transactions.Add(depositToBenefAccount); beneficiaryAcc.Balance = beneficiaryAcc.Balance + Math.Round(amount, 2); //add money to beneficiary } EFData.context.SaveChanges(); lblBalance.Content = "$ " + currentAccount.Balance; string message = string.Format("The {0} was completed successfully", currentTransType.ToLower()); MessageBox.Show(message, "Success", MessageBoxButton.OK, MessageBoxImage.Information); tbAmount.Text = ""; tbBenefAccNo.Text = ""; Receipt receiptDlg = new Receipt(currentAccount, previousBalance, transac, currentUser, true); receiptDlg.Owner = this; bool?result = receiptDlg.ShowDialog(); if (result == true) { MessageBoxResult answer = MessageBox.Show("Would you like to perform another " + currentTransType.ToLower() + " ?", "Choice required", MessageBoxButton.YesNo, MessageBoxImage.Question); if (answer == MessageBoxResult.No) { DialogResult = true; } } } catch (DbEntityValidationException ex) { var error = ex.EntityValidationErrors.First().ValidationErrors.First(); MessageBox.Show(error.ErrorMessage); EFData.context.Entry(transac).State = EntityState.Detached; EFData.context.Entry(depositToBenefAccount).State = EntityState.Detached; return; } catch (SystemException ex) { MessageBox.Show("Database error: " + ex.Message, "Database operation failed", MessageBoxButton.OK, MessageBoxImage.Error); return; } }