SQLDataReader Row Count
SQLDataReaders are forward-only. You're essentially doing this:
count++; // initially 1
.DataBind(); //consuming all the records
//next iteration on
.Read()
//we've now come to end of resultset, thanks to the DataBind()
//count is still 1
You could do this instead:
if (reader.HasRows)
{
rep.DataSource = reader;
rep.DataBind();
}
int count = rep.Items.Count; //somehow count the num rows/items `rep` has.
This will get you the row count, but will leave the data reader at the end.
dataReader.Cast<object>().Count();
DataTable dt = new DataTable();
dt.Load(reader);
int numRows= dt.Rows.Count;