try
{
sqlCommandWithdraw.Connection.Open();
sqlCommandWithdraw.Parameters["@cardNumber"].Value = Class1.cardNumber;
readdata = sqlCommandWithdraw.ExecuteReader();
while (readdata.Read())
{
balanceDB = decimal.Parse(readdata["balance"].ToString());
}
decimal withdrawAmm = Convert.ToDecimal(textWithdraw.Text);
balanceDB = balanceDB - withdrawAmm;
sqlCommandWithdraw.Connection.Close();
sqlCommandUpdate.Connection.Open();
sqlCommandUpdate.Parameters["@cardNumber"].Value = Class1.cardNumber;
sqlCommandUpdate.Parameters["@balanceDB"].Value = Class1.cardNumber;
readdata = sqlCommandUpdate.ExecuteReader();
MessageBox.Show(balanceDB +" Successfully Withdrawn");
}
I’m working on code for an ATM machine I’m a bit stuff on the withdraw it looks fine but doesn’t seem to change the balance to reflect the withdrawals in the database
My commands go like this (update)
update dbo.Accounts
set balance = @balanceDB
from dbo.ATMCards
INNER JOIN dbo.Accounts ON dbo.ATMCards.accountID = dbo.Accounts.accountID
where (dbo.ATMCards.cardNumber = @cardNumber)
and this is my command to select the data
select dbo.Accounts.balance
from dbo.ATMCards
INNER JOIN dbo.Accounts ON dbo.ATMCards.accountID = dbo.Accounts.accountID
where (dbo.ATMCards.cardNumber = @cardNumber)
Seems to run just fine added the message box to check it thanks for any help appreciate it!
You are passing the credit card number to the parameter
@balanceDB– this is the first mistake. Second, you do not useExecuteReaderto perform updates – useExecuteNonQueryinstead.EDIT
I’ll do some clean-ups for you:
}