Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Row Count from a DataReader

Status
Not open for further replies.

Dross

Programmer
Aug 16, 2001
212
US
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!
 
posted solution in thread855-530670

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top