troubleshooting Question

Issue with reading empty SQL table or nonexistent values

Avatar of G F
G FFlag for Canada asked on
C#MySQL Server
6 Comments1 Solution100 ViewsLast Modified:
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!

General Manager

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros