G F

G F

  • NA
  • 44
  • 6.6k

Problem pulling value from empty table

Feb 29 2020 9:06 PM
Hi,
 
So I need to pull a few values out of one row in a table, but the code I have skips a crucial part of the program (where the values are added together with other variables) if the table is empty.
 
Here is the code I'm working with:
string newIDtemp;
newIDtemp = txtBox_TD_Current_ID.Text;
string constring = "datasource=localhost;port=3306;username=root;pwd=;";
string Query = "SELECT * FROM bbb.approvaltotal WHERE TDId= '" + newIDtemp + "' ;";
MySqlConnection conDataBase3 = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase3);
MySqlDataReader myReader;
try
{
MessageBox.Show(tdapprovalcurrentjurfee.ToString());
MessageBox.Show(tdapprovalcurrentpnfee.ToString());
MessageBox.Show(tdapprovalcurrenttotal.ToString());
conDataBase3.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string approvaltotalid = myReader.GetString("Id");
string approvaltotaltdid = myReader.GetString("TDId");
string approvalpermitnowtotal = myReader.GetString("PermitNowTotal");
string approvaljurisdictiontotal = myReader.GetString("JurisdictionTotal");
.
.
.
.
Basically, if the table returns Null, the While(myReader.Read()) does not execute.
 
I just need to pull the values out of the table, add some decimals to them, and then put them back in to the same spots. Like an update but with addition.
 
Is there a better way than using a try-while statement? The reason i'm using it is because its working for other parts of my program, but wasn't aware the while skips if the reader is empty.
 
I've read on other sites that it's better to use Execute Scalar, if it's just one row, but I'm having a hard time finding a good example that fits my situation.
 
Any help would be appreciated!
 
G

Answers (4)