how to bind an query to a gridview programmatically?

  • Thread starter Thread starter Luc
  • Start date Start date
L

Luc

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()
 
Back
Top