Here is the code i came up with::
reader = cmd.ExecuteReader();
reader.Read();
if (reader.Read())
intQ = int.Parse(reader[0].ToString());
else
intQ = 0;
txtblck.Text = intQ.ToString();
reader.Close();
But this causes it to always execute the else, and if i do this:
reader = cmd.ExecuteReader();
if (reader.Read())
intQ = int.Parse(reader[0].ToString());
else
intQ = 0;
txtblck.Text = intQ.ToString();
reader.Close();
The if always return true, how should do this?
reader.Read()advances the reader to the next record, where the reader is initially set to before the first record. If callingreader.Read()returns false this means that it was unable to advance to the next record (i.e. the current record is the last record).This means that if you wish to read the first record you need to call
reader.Read()once, and ifreader.Read()returns false it means there were no records – like so:FYI
int.Parsewill throw an exception if the first record is null – this is different from having zero rows. Perhaps you should check for null values, or useint.TryParseinstead.