problem with connecting gridview with data in code-behind

Discussion in 'ASP .Net' started by Dan, May 20, 2009.

  1. Dan

    Dan Guest

    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()
     
    Dan, May 20, 2009
    #1
    1. Advertising

  2. Dan

    sloan Guest

    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.





    "Dan" <> wrote in message
    news:...
    > 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()
    >
     
    sloan, May 20, 2009
    #2
    1. Advertising

  3. Dan

    Dan Guest

    thanks

    "sloan" <> schreef in bericht
    news:%23C7%...
    > 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.
    >
    >
    >
    >
    >
    > "Dan" <> wrote in message
    > news:...
    >> 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()
    >>

    >
    >
     
    Dan, May 20, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    590
    Alphonse Giambrone
    Jun 28, 2003
  2. Daniel Manes

    Code-Behind Pain in the Behind!

    Daniel Manes, Jun 10, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    667
  3. Greg W via DotNetMonster.com

    problem connecting to my database behind a firewall

    Greg W via DotNetMonster.com, Jul 16, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    546
    Alan Samet
    Jul 19, 2005
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    489
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. Luc
    Replies:
    3
    Views:
    2,544
Loading...

Share This Page