Ajs135
asked on
c# Using The Select Command To display Info From The database
im trying to create a receipt that shows the transaction that the user did. in this case its a withdrawal transaction.
the withdrawal transaction works fine, the problem when its time to display the transaction.
when i try to display the new balance that the user has after the withdrawal, it will still display the balance before the withdrawal was made and not the new balance.
the withdrawal transaction works fine, the problem when its time to display the transaction.
when i try to display the new balance that the user has after the withdrawal, it will still display the balance before the withdrawal was made and not the new balance.
public static void Wdrwl(string name)
{
string UsName = name;
OleDbCommand cmd = DbUtils.GetCommand();
cmd.CommandType = CommandType.Text;
OleDbCommand cmdSelect = DbUtils.GetCommand();
cmdSelect.CommandType = CommandType.Text;
cmdSelect.CommandText = ("Select [Balance] from [Atm_Customers] WHERE UserName = '"+UsName+"'");
OleDbDataReader reader = cmdSelect.ExecuteReader();
//Console.WriteLine(UsName);
Console.Write("how much to withdrawal: ");
string mny = Console.ReadLine();
Convert.ToInt32(mny);
if(reader.Read() == true)
{
if((Convert.ToInt32( mny)) <= Convert.ToInt32(reader[0]))
{
// Add 'name' etc.
cmd.CommandText = ("UPDATE [Atm_Customers] SET [Balance] =([Balance] -@b) WHERE UserName= '"+UsName+"'");
cmd.Parameters.AddWithValue("@b", mny);
cmd.ExecuteNonQuery();
PrintReceipt.PrntRcp(UsName, mny); //print receipt
Console.Read();
}
else
{
Console.WriteLine("Insufficient Funds");
Console.Read();
Wdrwl(UsName);
}
}
//
public static void PrntRcp ( string Usn, string with )
{
string User = Usn;
string withdrwl = with;
Console.WriteLine(withdrwl); //testing
Console.WriteLine(User); // testing
OleDbCommand cmd = DbUtils.GetCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = ("Select FirstName,LastName,Balance from Atm_Customers WHERE UserName = '" + User + "'");
OleDbDataReader reader = cmd.ExecuteReader();
string fnn = string.Empty;
while (reader.Read())
{
fnn = reader["FirstName"].ToString() +" "+ reader["LastName"].ToString();
Console.WriteLine("balance: " + reader["Balance"].ToString());
Console.WriteLine(fnn);
Console.Read();
}
}
ASKER
the database is updating fine, it just when it comes to displaying on screen.
theres 4000, i withdrawal 500, on the database shows 3500 left. but on the screen it showing 4000
thats the issue im having
theres 4000, i withdrawal 500, on the database shows 3500 left. but on the screen it showing 4000
thats the issue im having
Are you saying that the Console.Writeline is showing a balance of 4000, or are you calling this function from a form via a button, and the form's data is not being updated?
If you are calling it from a form, and you need the form to update then both of your functions should be returning an object that you can use to update the form.
Please clarify this in order for us to help you further.
If you are calling it from a form, and you need the form to update then both of your functions should be returning an object that you can use to update the form.
Please clarify this in order for us to help you further.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yea that it , i wasnt closing the connection , after executing the query.
are you sure the update statement works fine. have you checked if the rows returned after the update is only one and not multiple?
For this - cmdSelect.CommandText = ("Select [Balance] from [Atm_Customers] WHERE UserName = '"+UsName+"'");
OleDbDataReader reader = cmdSelect.ExecuteReader();
you can actually use cmdSelect.ExecuteScalar() instead of reader.