Click here to Skip to main content
15,923,374 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how would i display the balance from my database. I have tried the following code but i am having no luck getting it to work.

C#
SqlDataReader readdata;
          
                sqlCommandCheckBalance.Connection.Open();
                sqlCommandCheckBalance.CommandText = "SELECT * FROM Accounts WHERE accountID = '" + "accountID" + "'";  
                readdata = sqlCommandCheckBalance.ExecuteReader();
                string balanceDB = null;
                while (readdata.Read())
                {
                    balanceDB = readdata["balance"].ToString();
                }
                balanceShow.Text += " " + balanceDB.ToString();
Posted

1 solution

Take off the spare double quotes:
C#
sqlCommandCheckBalance.CommandText = "SELECT * FROM Accounts WHERE accountID = '" + "accountID" + "'";
Becomes
C#
sqlCommandCheckBalance.CommandText = "SELECT * FROM Accounts WHERE accountID = '" + accountID + "'";
Or (preferably) use a parametrized query instead!


like this? sqlCommandCheckBalance.Parameters["accountID"].Value = balanceShow.Text;

And

on this line of code - readdata = sqlCommandCheckBalance.ExecuteReader();
cannot convert parameter from string to int32... how would i convert it try a few things but nothing working



That is SQL complaining about the value of a parameter you have passed over - presumably the account ID.
Check the type you gave it when you declared it, or try this:
C#
sqlCommandCheckBalance.CommandText = "SELECT * FROM Accounts WHERE accountID = @ID";
sqlCommandCheckBalance.Parameters.AddWithValue("@ID", accountID);
I don't guarantee that will compile, because I don't know type sqlCommandCheckBalance is - if it is an SqlCommand object it should be fine.
BTW: You do realise you can specify both the command and connection in the SqlCommand constructor?
C#
SqlCommand sqlCommandCheckBalance = new SqlCommand("SELECT * FROM Accounts WHERE accountID = @ID", con);
sqlCommandCheckBalance.Parameters.AddWithValue("@ID", accountID);


A lot of people think it is a lot tidier way to do it!
 
Share this answer
 
v2
Comments
stefanere2k9 24-Feb-12 12:41pm    
like this? sqlCommandCheckBalance.Parameters["accountID"].Value = balanceShow.Text;
stefanere2k9 24-Feb-12 12:48pm    
on this line of code - readdata = sqlCommandCheckBalance.ExecuteReader();
cannot convert parameter from string to int32... how would i convert it try a few things but nothing working
stefanere2k9 7-Mar-12 13:53pm    
managed to get most of the code working now
SqlDataReader readdata;

{
sqlCommandbalance.Connection.Open();
sqlCommandbalance.Parameters["@accountID"].Value = _cardNumber;
readdata = sqlCommandbalance.ExecuteReader();
string balanceDB = null;
while (readdata.Read())
{
balanceDB = readdata["balance"].ToString();
}
sqlCommandbalance.Connection.Close();
sqlCommandbalance.Dispose();
balanceShow.Text += " " + balanceDB.ToString();
}
on the last line balanceShow.Text += " " + balanceDB.ToString(); i got a error saying Object reference not set to an instance of an object.
OriginalGriff 7-Mar-12 15:03pm    
Check balanceShow is set to a value and not null. If it is, then your reader is returning no records as balanceDB is null.
At a guess, your _cardNumber is not a valid account ID.
stefanere2k9 7-Mar-12 15:08pm    
_cardNumber is the value from the previous form

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