In this article I will explain with an example, how to populate (bind) Repeater control using SqlDataSource in ASP.Net.
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
HTML Markup
The following HTML Markup consists of an ASP.Net Repeater control and ASP.Net SqlDataSource control.
The SqlDataSource control is set with the following properties.
1. ConnectionString – Name of the Connection String setting in the Web.Config file.
2. SelectCommand – The Select statement to fetch the records from the Customers table of the Northwind database.
The ID of the SqlDataSource control is set as DataSourceID of the Repeater control.
<asp:Repeater ID="rptCustomers" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 80px">
Customer Id
</th>
<th scope="col" style="width: 120px">
Customer Name
</th>
<th scope="col" style="width: 100px">
Country
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>'/>
</td>
<td>
<asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>'/>
</td>
<td>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>"
SelectCommand="SELECT TOP 10 * FROM Customers"></asp:SqlDataSource>
Screenshot
Demo
Downloads