What's wrong???

G

Guest

Can someone please tell me what is wrong with the following code!

I have a record in the datareader but it isn't showing up and niether is the
label... I presume there is a problem with this code!

Thanks

...CODE..
Dim myReader As SqlDataReader = cmd.ExecuteReader()
Dim hasRows As Boolean
hasRows = False
DGPages.DataSource = myReader
While myReader.Read()
hasRows = True
If hasRows = True Then
DGPages.DataBind()
ElseIf hasRows = False Then
lblNoContent.Visible = True
lblNoContent.Text = "There a currently no pages for"
End If
End While

myReader.Close()
 
C

Chris Taylor

Hi,

I never really tested this, but my guess would be as follows:
Since DataReaders are forward only, your call to DataReader.Read is moving
the reader cursor forward, then
when you call the DataBind method it will also cause the DataGrid to move
through the same reader, which has already moved on to the next record. In
your case there is no next record so the DataGrid is empty.

Hope this helps
 
G

Guest

OK... Got it and I think you are right but any ideas on how to get around
this issue???

Thanks
 
G

Guest

This is just a guess because I don't write VB

Dim myReader As SqlDataReader = cmd.ExecuteReader()
Dim hasRows As Boolean
hasRows = False
DGPages.DataSource = myReader
DGPages.DataBind()

If DGPages.Rows.Count = 0 Then
lblNoContent.Visible = True
lblNoContent.Text = "There a currently no pages for"
End If

myReader.Close()
 
M

Mark Fitzpatrick

Chris is right, what you're doing is binding the datagrid repeatedly to the
datareader. For every row of the datareader you are calling the databind
method, thus constantly updating the grid. You don't have to ever call the
read method when you are binding directly to the datareader. If you're using
ASP.Net 1.1, There is already a HasRows property for the datareader that you
can check and avoid the other steps alltogether. Just check the hasrows
property of the datareader then bind if true. Just don't call the read
method before binding as that will read the first row.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top