I am trying to get the number of rows that were returned by iterating the reader. But I always get 1 when I run this code? Did I screw up something in this?
int count = 0;
if (reader.HasRows)
{
while (reader.Read())
{
count++;
rep.DataSource = reader;
rep.DataBind();
}
}
resultsnolabel.Text += " " + String.Format("{0}", count) + " Results";
This question is related to
c#
sql
sqldatareader
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;
Maybe you can try this: though please note - This pulls the column count, not the row count
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int count = reader.VisibleFieldCount;
Console.WriteLine(count);
}
}
Source: Stackoverflow.com