473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Data Reader

Is there any way to know how many records a data reader returns, without
having to read each one and increment a counter?

I am creating an ASP.NET 2.0 project in C#.

I need to read data from the first record, if there is only one and count
them if there is more than one. The code that I have written looks like this:

dr = cd.ExecuteReader();
if (dr.Read())
{
intPatientCount=1;
strPatNo = Convert.ToString(dr["pat_no"]); // Store the pat_no in
case there is only one
while (dr.Read())
{
intPatientCount++;
}
}
dr.Close();

It works, but it's not very elegant or efficient. Can anyone suggest a
better way?

Thanks,
Helen

Jan 31 '07 #1
1 873

"Helen Trim" <Helen Tr**@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
Is there any way to know how many records a data reader returns, without
having to read each one and increment a counter?

I am creating an ASP.NET 2.0 project in C#.

I need to read data from the first record, if there is only one and count
them if there is more than one. The code that I have written looks like
this:

In general, I don't know of a better way. However, if you are dealing with
database access, it may be far more efficient to run a "SELECT COUNT(*)
WHERE...." query first to get the count, then get the actual data only if
the count is 1.
>
dr = cd.ExecuteReader();
if (dr.Read())
{
intPatientCount=1;
strPatNo = Convert.ToString(dr["pat_no"]); // Store the pat_no in
case there is only one
while (dr.Read())
{
intPatientCount++;
}
}
dr.Close();

It works, but it's not very elegant or efficient. Can anyone suggest a
better way?

Thanks,
Helen

Feb 7 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mike L | last post by:
I tried a sample of code in MSDN magazine, but now I'm stuck. What code has the best performance to populate a Data Grid with a SP? Below is the code I have, which might be completing the wrong...
2
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row...
2
by: Joe | last post by:
Anyone can suggest the best method of reading XML and adding data to ListView? Here is the xml data structure:: <xml> <site> <url>http://www.yahoo.com</url> <lastupdate></lastupdate>...
1
by: weird0 | last post by:
I have created a GridView and dynamically added data to it by creating a DataTable(as advised) and bound it with a reader. Then, assigned the DataSource of GridView to DataTable. But even that aint...
1
by: dotnetnovice | last post by:
Hi everybody... I want to show data in the data gridview placed in my form after getting data from my data access layer class through a class... Here is my code in the data access layer class....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.