problem with connecting gridview with data in code-behind

D

Dan

Hi,

i defined a gridview in aspx file and i try to connect it to a datasource in
code-behind (vb.net).
i know there are records (tested with response.write).
The problem is that the gridview remains invisible because no records come
into it.
Here is the code:
Thanks
Dan

code in aspx file
----------------
<asp:GridView ID="GridView1" runat="server" >
</asp:GridView>


code-behind
-------------
Dim bf As New BoundField
Dim dtreader As SqlDataReader
.....
.....
comd.CommandText = "select email from email"
dtreader = comd.ExecuteReader
If dtreader.HasRows Then
While dtreader.Read()
dtreader.GetString(0)
'tested with response.write
End While
End If
bf.DataField = "email"
bf.HeaderText = "email"
GridView1.Columns.Add(bf)
GridView1.DataSource = dtreader
GridView1.DataBind()
 
S

sloan

An IDataReader is a firehose method for dataaccess. Once the firehose is
"rolled out", you're at the end of it.

Once you run this:
If dtreader.HasRows Then
While dtreader.Read()
dtreader.GetString(0)
'tested with response.write
End While
End If

the firehose is at end..........that's why your next lines of binding don't
work.


As a test, get rid of this code:
If dtreader.HasRows Then
While dtreader.Read()
dtreader.GetString(0)
'tested with response.write
End While
End If


and ~~then~~ bind.


You might want to google
N Layered Development

Because that code is deficient when it comes to best practices.
 
D

Dan

thanks

sloan said:
An IDataReader is a firehose method for dataaccess. Once the firehose is
"rolled out", you're at the end of it.

Once you run this:
If dtreader.HasRows Then
While dtreader.Read()
dtreader.GetString(0)
'tested with response.write
End While
End If

the firehose is at end..........that's why your next lines of binding
don't work.


As a test, get rid of this code:
If dtreader.HasRows Then
While dtreader.Read()
dtreader.GetString(0)
'tested with response.write
End While
End If


and ~~then~~ bind.


You might want to google
N Layered Development

Because that code is deficient when it comes to best practices.
 

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

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top