how to bind an query to a gridview programmatically?

Discussion in 'ASP .Net' started by Luc, Jul 20, 2010.

  1. Luc

    Luc Guest

    Hi,

    i want to bind programmatically the records of a select query to an existing
    gridview. I can get the records via dtreader, but i don't know how to get
    them into the gridview (which is created in the aspx file)

    Thanks
    Luc

    My attempt:

    connectionstr =
    ConfigurationManager.ConnectionStrings("myconn").ConnectionString.ToString()
    connection = New SqlConnection(connectionstr)
    comd = New SqlCommand()
    comd.Connection = connection
    comd.CommandText = "select field1 from table1"
    connection.Open()

    dtreader = comd.ExecuteReader
    If dtreader.HasRows Then
    While dtreader.Read()
    lc = dtreader.GetString(0)
    ????
    End While
    End If

    GridView1.DataSource = ???
    GridView1.DataBind()
     
    Luc, Jul 20, 2010
    #1
    1. Advertising

  2. Luc

    Luc Guest

    i found it:

    GridView1.DataSource = dtreader

    "Luc" <ll@nospam> schreef in bericht
    news:O1%...
    > Hi,
    >
    > i want to bind programmatically the records of a select query to an
    > existing gridview. I can get the records via dtreader, but i don't know
    > how to get them into the gridview (which is created in the aspx file)
    >
    > Thanks
    > Luc
    >
    > My attempt:
    >
    > connectionstr =
    > ConfigurationManager.ConnectionStrings("myconn").ConnectionString.ToString()
    > connection = New SqlConnection(connectionstr)
    > comd = New SqlCommand()
    > comd.Connection = connection
    > comd.CommandText = "select field1 from table1"
    > connection.Open()
    >
    > dtreader = comd.ExecuteReader
    > If dtreader.HasRows Then
    > While dtreader.Read()
    > lc = dtreader.GetString(0)
    > ????
    > End While
    > End If
    >
    > GridView1.DataSource = ???
    > GridView1.DataBind()
    >
     
    Luc, Jul 20, 2010
    #2
    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. John Hoge

    Bind multiple selections to a query

    John Hoge, Nov 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,202
    John Hoge
    Nov 30, 2004
  2. mr2_93
    Replies:
    1
    Views:
    3,644
    mr2_93
    Oct 2, 2005
  3. Niclas
    Replies:
    1
    Views:
    422
    Patrick.O.Ige
    Jun 24, 2006
  4. Replies:
    0
    Views:
    603
  5. Beatifull
    Replies:
    0
    Views:
    393
    Beatifull
    Nov 27, 2007
Loading...

Share This Page