I have a DataReader that has executed correctly, but now I need to count the rows and that count determines what I do next. Does anyone know how to get a simple row count from a DataReader?
Why would you want to do this. The DataReader is, essentially, like a forward only recordset. You can get the number of rows with...
<CODE>
int i = 0;
while (drCounterparty.Read())
{
i +=1;
}
</CODE>
but you can only access the reader whilst the connection to the database is open. Wouldn't you be better of pulling the data into a DataTable, which has a Rows collection, which itself has a Count property, giving the information you want...
...or you could perform the COUNT as an SQL query, returning the value by SqlCommand.ExecuteScalar?
Little confused as to what you're trying to achieve here...
Rhys
Thought out... Maybe,
Opinionated... Probably
But it is only an opinion!
What I am trying to do is after the sql is generate, if the row count is greater than 1 then bind it to a data grid else pass a specific field from the query to another page to execute a different query
Use a DataTable, it'll be far far simpler, and i don't believe you can bind a DataReader to anything, it does what it says on the tin, reads.
Use a DataAdaptor to populate a DataTable, check the Count property of the Rows collection of the DataTable and if it's >1 set the source of the datagrid to the datatable, and bind it else do 'y'.
Make Sense...
Rhys
Thought out... Maybe,
Opinionated... Probably
But it is only an opinion!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.