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

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
 
M

Mr. Arnold

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
 
R

Ricardo Silva

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/>
 
L

Luc

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/>
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top