public SqlDataReader ReturnResults(){ ... =09 SqlDataReader dr =3D
myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
=09
MyDataGrid.DataSource =3D ReturnResults();
MyDataGrid.DataBind();
myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
=09
MyDataGrid.DataSource =3D ReturnResults();
MyDataGrid.DataBind();
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) 'Create connection object
Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("DSN_aspa")) 'Create command object
Dim myCommand As New SqlCommand("SELECT PersonName,PersonAge,PersonGender FROM tblPeople", myConnection) 'Create SqlDataReader
Dim myDataReader As SqlDataReader Try
'Open Connection
myConnection.Open() 'Fill myDataReader using ExecuteReader method.
'Close connection using CommandBehavior.CloseConnection enumeration
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Set datasource and bind DataGrid to datasource
myDataGrid.DataSource = myDataReader
myDataGrid.DataBind()
Catch myException As Exception
Response.Write("An error has occurred: " & myException.ToString())
Finally
If Not myDataReader Is Nothing Then
'Close SqlDataReader
myDataReader.Close()
End If
End Try
End Sub</script>
<html>
<body>
<form runat="server">
<asp:datagrid id="myDataGrid"
runat="server"
AutoGenerateColumns="false" > <columns>
<asp:templatecolumn headertext="Row Number">
<itemtemplate>
<span><%# Container.ItemIndex+1 %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="Name">
<itemtemplate>
<span><%# Container.DataItem("PersonName") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="Age">
<itemtemplate>
<span><%# Container.DataItem("PersonAge") %></span>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="Gender">
<itemtemplate>
<span><%# Container.DataItem("PersonGender") %></span>
</itemtemplate>
</asp:templatecolumn>
</columns> </asp:datagrid>
</form>
</body>
</html>