HasRows logic flaw

Well maybe the title of this post is a bit strong. It's not really a flaw, but something occurs to me today using this new Datareader property.

If you use Hasrows it doesn't mean that you have indeed read any data !

I know it looks strange, but Hasrows is just there to check that some data are in your reader.

What is the point to open a datareader, tell me that I have rows, and not reading the data ;-)

Where is the logic of this ?

So unless someone tell me I am a stupid man (and I am surely going to receive hundreds of comments with something like that ;-)) I stay with the 'old' version of my code which test and read the first record in the same time:

If MyDataReader.Read then

...

End If

So if someone has a real use for Hasrows, tell me !

11 Comments

  • Of course the Read logic is correct. The issue is that if you want to check the status in a non-destructive way (a way that does not eat up a record) there was not a way to do so. HasRows does allow that.

  • OK Douglas, but what is the point to consume a data reading if it's just to see if you have rows ?

    Maybe to retrieve a recordcount, but others methods exist for that.

  • Yup, was gonna say that, but Douglas beat me to it...I was actually pretty surprised to see HasRows...once you get used to the way the DataReader works, there really isn't a need for it.



    Only time I ever needed it was determining whether or not to display a list control in ASP.NET if there are records or not. It was easy to work around though.



    rpMain.DataSource = MyReader

    rpMain.DataBinding()



    If rpMain.Items.Count > 0 Then

    'hide repeater and show label

    Else

    'show repeater and hide label

    End If



    I guess now instead, you could save the actual binding code like this...



    If MyReader.HasRows Then

    rpMain.DataSource = MyReader

    rpMain.DataBind()

    'show repeater and hide label

    Else

    'hide repeater and show label

    End If



    *shrug*

  • sorry about that first code example in the last comment...i'm dislexic ;)

  • Erik that exactly what I was saying initially :-)

    What is the point to add useless and confusing functions ??



    And more annoying, especially for newbies, is that if you use an hasrows in if then else, not knowing that this is not a read method, the error message coming immediately after an attempt to read some data is not obviously linked to the HasRows.

  • I think the advantage is that you can make a decision based upon whether records exist, and then, say, pass the DataReader on to another object to actually read the data. Other ways of getting the row count short of reading the DataReader are not that great (OUTPUT parameters from Stored Procs, etc.)



    I personally have not found the lack of HasRows to be a problem (I use a pattern as above, checking Items.Count) but it is a little easier to have this available as an option.

  • Well hey, Doug and I gave you reasons why it's needed, it's just that those situations don't happen very often. ;) I personally haven't used HasRows yet in any of my projects.

  • It could be quite useful when you have Set Nocount on in your stored proc's, but you still want to know if the datareader has rows.



    Our SQL DBA's default to set nocount on, as according to them it saves some network packets.

  • The reason you need HasRows is in the case of where you need to check if the there are any records before you bind the SqlDataReader to a DataGrid WebControl. If you use the Read method, then bind, you will be missing the first record...



    For example, say you get obtain a SqlDataReader as follows:



    Dim connString As String = "Server=(local);Database=Northwind;UID=sa;PWD=myPassword"

    Dim sqlConn As SqlClient.SqlConnection = New SqlClient.SqlConnection(connString)

    Dim sqlSelectCmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("Select * From Customers", sqlConn)



    sqlConn.Open()

    Dim sqlDataReader As SqlClient.SqlDataReader = sqlSelectCmd.ExecuteReader(CommandBehavior.CloseConnection)



    Now if you use the read method, then bind, you will be missing the first record.



    If sqlDataReader.Read Then

    DataGrid1.DataSource = sqlDataReader

    DataGrid1.DataBind()

    Else

    Label1.Text = "No records found"

    End If



    However, if you use the HasRows property, then you won't be missing the first record.



    If sqlDataReader.HasRows Then

    DataGrid1.DataSource = sqlDataReader

    DataGrid1.DataBind()

    Else

    Label1.Text = "No records found"

    End If



  • Hmmm ! Not really what I use for a Datagrid bind. Instead I use a Dataadapter and Datatables, so Hasrows has no use for me there.



    Datareader is more useful for a Repeater or to fill a form

  • This conversation is old, but I was doing a search on "How to check if a datareader has data in it". And up came this page in Google!



    I want to check if there are any rows and if there aren't then I pop up a message if there are then I don't want to read it yet (similar to what somebody previously said). The only way to do it is to use HasRows. I think it is very useful.



    Z.

Comments have been disabled for this content.