private void AddTransactionBtn_Click(object sender, EventArgs e) { AddTransactionForm frm = new AddTransactionForm(user); if (frm.ShowDialog(this) == DialogResult.OK) { TransactionModel transactionModel = new TransactionModel(frm.transaction); double value = (transactionModel.Type == "Income") ? transactionModel.Value : -transactionModel.Value; string insertString = $"INSERT INTO transactions VALUES (" + $"'{transactionModel.Username}'," + $"'{transactionModel.WalletName}'," + $"'{transactionModel.CategogyName}'," + $"'{transactionModel.Type}'," + $"{value}," + $"'{transactionModel.Note}'," + $"'{transactionModel.Time.ToString("yyyy-MM-dd")}')"; try { Connection.Connect(); Connection.Insert(insertString); double amount = (double)Connection.SelectScalar($"SELECT amount FROM wallets WHERE username='******' AND walletName='{transactionModel.WalletName}'"); double finalAmount = amount + value; Connection.Update($"UPDATE wallets SET amount={finalAmount} WHERE username='******' AND walletName='{transactionModel.WalletName}'"); Connection.Close(); TransactionShowContent(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
private void ModifyTransaction(object sender, MouseEventArgs e) { AddTransactionForm frm = new AddTransactionForm(user); frm.label1.Text = "UPDATE TRANSACTION"; frm.button1.Visible = false; string type = (sender as Transaction).Value.Text; double finalAmount = Convert.ToDouble(type); if (Convert.ToDecimal(type) > 0) { frm.income.Checked = true; frm.expense.Checked = false; frm.value.Value = Convert.ToDecimal((sender as Transaction).Value.Text); } else { frm.income.Checked = false; frm.expense.Checked = true; frm.value.Value = -Convert.ToDecimal((sender as Transaction).Value.Text); } frm.income.Enabled = false; frm.expense.Enabled = false; frm.wallet.Text = (sender as Transaction).Wallet.Text; frm.wallet.Enabled = false; frm.note.Text = (sender as Transaction).note; frm.time.Value = DateTime.ParseExact((sender as Transaction).Time.Text, "dd-MM-yyyy", null); if (frm.ShowDialog(this) == DialogResult.OK) { TransactionModel transactionModel = new TransactionModel(frm.transaction); double value = (transactionModel.Type == "Income") ? transactionModel.Value : -transactionModel.Value; string updateString = $"UPDATE transactions SET " + $"transactionValue={value}," + $"transactionNote='{transactionModel.Note}'," + $"transactionTime='{transactionModel.Time.ToString("yyyy-MM-dd")}'" + $"WHERE id={(sender as Transaction).id}"; try { Connection.Connect(); Connection.Update(updateString); if (finalAmount != transactionModel.Value) { double amount = (double)Connection.SelectScalar($"SELECT amount FROM wallets WHERE username='******' AND walletName='{transactionModel.WalletName}'"); double a = amount - finalAmount + value; Connection.Update($"UPDATE wallets SET amount={a} WHERE username='******' AND walletName='{transactionModel.WalletName}'"); Connection.Close(); } TransactionShowContent(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }