I want to be able to display balance to label and my code is not working. This is code I got so far:
SqlDataReader readdata;
{
sqlCommandbalance.Connection.Open();
sqlCommandbalance.Parameters["@accountID"].Value = show.accoundID;
readdata = sqlCommandbalance.ExecuteReader();
string balanceDB = null;
while (readdata.Read())
{
balanceDB = readdata["balance"].ToString();
}
sqlCommandbalance.Connection.Close();
balanceShow.Text += " " + balanceDB.ToString();
}
On this line – balanceShow.Text += " " + balanceDB.ToString(); got a error saying Object reference not set to an instance of an object.
You’re calling
balanceDB.ToString(), whenbalanceDBwill be null if the data reader contains no rows.Note that
balanceDB.ToString()is redundant, sincebalanceDBis already a string. Just take out theToStringcall.You could also initialize balanceDB as