Bind data to a datalist control

This example will display a series of links from our sample database.

In this example we bind the data using a datalist control.

Code

<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">
sub Page_Load
'variable declarations
dim dbconnection,sqlstring,dbcommand,dbreader
'connect to our database
dbconnection=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("sampledb.mdb"))
'open a connection
dbconnection.Open()
'This is our sql, in this case select all items from the tblsample table
sqlstring="SELECT * FROM tblsample"
dbcommand=New OleDbCommand(sqlstring,dbconnection)
dbreader=dbcommand.ExecuteReader()
tblsample.DataSource=dbreader
tblsample.DataBind()
'close connection and reader objects
dbreader.Close()
dbconnection.Close()
end sub
</script>

<html>
<body>

<asp:Datalist id="tblsample" runat="server">
<HeaderTemplate>
<table border="0" width="100%">
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("url")%> </td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Datalist>

</body>
</html>