We help IT Professionals succeed at work.
Get Started

Issue with reading empty SQL table or nonexistent values

G F asked
Last Modified: 2020-03-02
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;



                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!

Watch Question
General Manager
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE