How Do I Determine if a Control Bound to a DataReader Has Rows?

If you use .NET Data Binding to bind data from a data source (like a DataSet) to a control (like a DataGrid or Repeater) you often want to know if the control contains any rows. You can then use this knowledge to, for example, hide the entire DataGrid and display a message with the text "No records found" instead. You can easily accomplish this with a DataSet, because you can retrieve the number of items in the DataSet using something like myDataSet.Tables[0].Rows.Count. But what if you use a DataReader like the SqlDataReader? This class does not expose a count property.... Fortunately, there is a way to work around this.

If all you're interested in is knowing if the DataReader contained at least one row, you can use the HasRows property of the DataReader. This property returns true if the DataReader has at least one row. You can use it to hide the DataGrid and display a label instead when the property is false, like this:

if (myDataReader.HasRows)
  // At least one row; bind the grid
  lblNoRecords.Visible = false;
  myDataGrid.DataSource = myDataReader;
  // No rows found; hide the grid and display the label
  myDataGrid.Visible = false;
  lblNoRecords.Text = "No records found"

Note that the HasRows property is only available since .NET 1.1. So, for a solution that works in .NET 1.0, we'll need to think of alternative ways to accomplish this.

The next solution I am going to demonstrate will work in both .NET 1 and .NET 1.1. Even better, not only does it tell you if there are any records found, it will also tell you how many. All you need to do is request the Count property of the Items collection of the databound control after you have bound it to the DataReader, as in the following example:

myDataGrid.DataSource = myDataReader;

if (myDataGrid.Items.Count > 0)
{ // At least one row; hide the label lblNoRecords.Visible = false; }
{ // No rows found. Hide the grid and display the label myDataGrid.Visible = false; lblNoRecords.Text = "No records found" }

In this example, I simply used the Count property to see if there was at least one record. However, you can also use this code to display the number of records found on the label, like this:

lblNumRecords.Text = myDataGrid.Items.Count.ToString() + 
" number of records found.";

Note that the Count property will return a useless value when you're using paging. When paging is enabled for the DataGrid control, the Count property returns the number of items currently displayed on the "page" of the control, which is always equal to the PageSize except for the last page. It will not return the number of items in the original datasource that the control is bound to.

Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .

Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.

Feedback by Other Visitors of Imar.Spaanjaars.Com

On Thursday, July 07, 2005 9:31:52 PM demivolt said:
Thanks for your reply to the wrox forum.  Did you see my rejoinder?  
On Monday, January 30, 2006 11:55:50 PM Richard Stelma said:
Thanks Imar. I love your site. Very much appreciated. It may seem insignificant to post small snippets of code but I've found that most of the problems I have in coding are resolved with only a few lines of code.
On Tuesday, January 31, 2006 7:12:38 AM Imar Spaanjaars said:
Hi Richard,

Thanks for your kind words.

That's often the reason why I post them. Not only for others, but also for myself. It's the little things or the exact code that I sometimes forget. However, if I know I have written something about, it's easy to locate it and paste it into an application.



Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.