Click here to Skip to main content
16,020,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey i want to be able to draw from my overdraft when my balance get to 0. here my code could you please direct me in the right direction :)
Posted
Updated 19-Mar-12 2:35am
v3

1 solution

You seem to be treating an overdraft as a separate account - this seems a little silly.

The way I would do it:
Have a method RequestFunds(amount)
In the method,
1) Add the overdraft limit to the current balance - this is the availableFunds.
2) Check if the requested amount is less than or equal to the availableFunds.
3) If it is, all fine - just go ahead, and subtract the amount from the balance.
4) If it isn't, then you need to report an error and refuse the transaction.
 
Share this answer
 
Comments
stefanere2k9 18-Mar-12 21:28pm    
i have got this code now but it dont seem to be working;
SqlDataReader readdata;
sqlCommandWithdraw.Connection.Open();
decimal balance = decimal.Parse(Txt_Withdraw.Text);
decimal availableTBalance = 0.00M;
sqlCommandWithdraw.Parameters["@accountID"].Value = showaccounts.accountID;
readdata = sqlCommandWithdraw.ExecuteReader();
while (readdata.Read())
{
availableTBalance = (Convert.ToDecimal(readdata["balance"].ToString()) + Convert.ToDecimal(readdata["overdraftlimit"]));
}

sqlCommandWithdraw.Connection.Close();

if (availableTBalance >= balance)
{
sqlCommandUpdateBalance.Connection.Open();
availableTBalance -= balance;
sqlCommandUpdateBalance.Parameters["@balance"].Value = availableTBalance;
sqlCommandUpdateBalance.Parameters["@accountID"].Value = showaccounts.accountID;
sqlCommandUpdateBalance.ExecuteNonQuery();
sqlCommandUpdateBalance.Connection.Close();
OriginalGriff 19-Mar-12 4:26am    
There are a couple of things wrong with that, but I don't know if they are relevant to the problem you are having:
1) Don't call it "balance" - it's not, it's the amount they customer wants to withdraw. Calling it "balance" rather than "withdrawlAmount" or similar makes teh code harder to read and understand.
2) Don't use decimal.Parse - use decimal.TryParse instead, and report any bad data to the user - otherwise you could be withdrawi8ng the wrong amount.
3) Don't use a while loop - if you are returning more than one record then you have a problem anyway, so check - with error reporting, and use "if" instead.
4) Be consistent! If "balance" needs ToString, then "overdraftlimit" needs ToString! In fact, neither of them do, and I would just cast them to decimal rather than using Convert.ToDecimal anyway.
5) If this was a genuine app, rather than a homework exercise, I would probably move this into a stored procedure, and lock the account record while I was doing it, as in a multiuser environment it would be possible to make several withdrawals at teh same time, and get them all oked, it the customer was lucky!

Other than that, I need to know what the problem you are having is, as "it dont seem to be working" doesn't tell me a lot about what it does or doesn't do! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900