I have a SQL Command that query’s a DB table and retrieves information….very basic.
I can see the reader has the results from the DB table when debugging, but for some reason it always skips on “While Reader.Read” and then skips down and closes the connection never reading the data.
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("lbx_EmailDomains_SELECT", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
// Never Reaches Here
}
}
reader.Close();
con.Close();
UPDATE: Removing the reader.HasRows fixed the issue.
My guess is that actually it’s not getting as far as
reader.Read(). I suspect thatreader.HasRowsis returning false, precisely because you haven’t calledreader.Read()yet.I don’t think the test for
HasRowsis useful at all, to be honest – I’d just get rid of it.Note that it would be better to use
usingstatements and get rid of the manualClose()calls, so that the connection/reader will be closed even in the event of an exception.EDIT: According to the comments:
HasRowswas apparently returningtruebeforereader.Read()returned falseHasRowstest apparently fixed the problem. Odd.I wonder whether there’s some debugger interaction we don’t know about here…