Questions That Should Be Asked More Frequently

Q

I’ve executed a query to create a SqlDataReader. How can I determine whether my query returned rows of data before I bind my SqlDataReader to my bound control?

A

This was a very common question for Web developers in version 1.0 of the .NET Framework. Since that time, thankfully, much has changed.

In ADO.NET 1.1, the DataReader classes added a HasRows property, which returns False if the query returned no rows of data. So you can check this property programmatically before binding and then take the appropriate action.

The new ASP.NET data-bound controls (GridView, FormView, and DetailsView) have properties that you can set (EmptyText, EmptyDataRowStyle, and EmptyDataTemplate) that dictate how the controls ...

Get Programming Microsoft® ADO.NET 2.0 Core Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.