how to bind data provided in code-behind to a gridview?

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

Luc

Hi,

I want to bind the data provided by a sql statement in code-behind to a
gridview defined in the aspx file (i know it's possible to define a
sqldatasource in aspx).
How can i do that with this code here below?
Thanks for help.
Luc

<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</form>
-------------------------------------
Imports System.Data.SqlClient
Partial Class a
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim connection As SqlConnection
Dim comd As SqlCommand
Dim connectionstr,stdnr As String
Dim dtreader As SqlDataReader
connectionstr =
ConfigurationManager.ConnectionStrings("myconn").ConnectionString.ToString()
connection = New SqlConnection(connectionstr)
comd = New SqlCommand()
comd.Connection = connection
connection.Open()
comd.CommandText = "SELECT login FROM mytable"
dtreader = comd.ExecuteReader
If dtreader.HasRows Then
While dtreader.Read()
stdnr = dtreader.GetValue(0)
End While
End If
connection.Close()
End Sub
 
Luc said:
Hi,

I want to bind the data provided by a sql statement in code-behind to a
gridview defined in the aspx file (i know it's possible to define a
sqldatasource in aspx).
How can i do that with this code here below?
Thanks for help.
Luc

<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</form>
-------------------------------------
Imports System.Data.SqlClient
Partial Class a
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim connection As SqlConnection
Dim comd As SqlCommand
Dim connectionstr,stdnr As String
Dim dtreader As SqlDataReader
connectionstr =
ConfigurationManager.ConnectionStrings("myconn").ConnectionString.ToString()
connection = New SqlConnection(connectionstr)
comd = New SqlCommand()
comd.Connection = connection
connection.Open()
comd.CommandText = "SELECT login FROM mytable"
dtreader = comd.ExecuteReader
If dtreader.HasRows Then
While dtreader.Read()
stdnr = dtreader.GetValue(0)
End While
End If
connection.Close()
End Sub

You look at the VB example in the link.

http://demos.telerik.com/aspnet-ajax/treeview/examples/programming/databinding/defaultcs.aspx

And look look at what is happening with the IBindableSource(), while you're
in that reader loop.

Then you bind the results to the control.

Those properties in the sitedataitem can be anything you want it to be.

http://www.vbdotnetheaven.com/Uploa...Net04192005060237AM/PropertiesInVbDotNet.aspx
 
Hi Luc,

you can do it like this,


Dim connection As SqlConnection
Dim comd As SqlCommand
Dim connectionstr As String
Dim dtreader As SqlDataReader
connectionstr =
ConfigurationManager.ConnectionStrings("myconn").ConnectionString.ToString()
connection = New SqlConnection(connectionstr)
comd = New SqlCommand()
comd.Connection = connection
connection.Open()
comd.CommandText = "SELECT login FROM mytable"
dtreader = comd.ExecuteReader
GridView1.DataSource = dtreader
GridView1.DataBind()
connection.Close()

**


ricardo silva blog Temporary Post Used For Theme Detection
(94b32f92-9771-4cb2-988b-ec374534f2d0 –
3bfe001a-32de-4114-a6b4-4005b770f6d7)
<http://devtipsbyricardosilva.wordpr...534f2d0-3bfe001a-32de-4114-a6b4-4005b770f6d7/>
 
Thanks

Ricardo Silva said:
Hi Luc,

you can do it like this,


Dim connection As SqlConnection
Dim comd As SqlCommand
Dim connectionstr As String
Dim dtreader As SqlDataReader
connectionstr =
ConfigurationManager.ConnectionStrings("myconn").ConnectionString.ToString()
connection = New SqlConnection(connectionstr)
comd = New SqlCommand()
comd.Connection = connection
connection.Open()
comd.CommandText = "SELECT login FROM mytable"
dtreader = comd.ExecuteReader
GridView1.DataSource = dtreader
GridView1.DataBind()
connection.Close()

**


ricardo silva blog Temporary Post Used For Theme Detection
(94b32f92-9771-4cb2-988b-ec374534f2d0 –
3bfe001a-32de-4114-a6b4-4005b770f6d7)
<http://devtipsbyricardosilva.wordpr...534f2d0-3bfe001a-32de-4114-a6b4-4005b770f6d7/>
 
Back
Top