In this article I will explain with an example, how to bind (populate) Repeater control using DataReader in ASP.Net using C# and VB.Net.
The records from the Database will be read using DataReader and then the data is copied into the Generic List collection of the Class objects, which is later used to populate the Repeater in ASP.Net using C# and VB.Net.
 
 
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Bind (Populate) Repeater using DataReader in ASP.Net using C# and VB.Net
 
I have already inserted few records in the table.
Bind (Populate) Repeater using DataReader in ASP.Net using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Namespaces
You will need to the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net Repeater control.
The Repeater control has the following templates:-
HeaderTemplate – The content of this template will not be repeated and will be placed in the top most position i.e. head section of the Repeater control.
ItemTemplate – The content of this template will be repeated for each record present in its DataSource.
AlternatingItemTemplate – AlternatingItemTemplate is used for adding alternate items. It is used along with ItemTemplate, generally for displaying a different design for alternating items.
SeparatorTemplate - This template is used to add a separator between two items of the Repeater control.
FooterTemplate – The content of this template will not be repeated and will be placed in the bottom most position i.e. footer section of the Repeater control.
<asp:Repeater runat="server" ID="rptCustomers">
    <HeaderTemplate>
        <table>
            <tr>
                <th>Customer Id</th>
                <th>Name</th>
                <th>Country</th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
            </td>
            <td>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
            </td>
            <td>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>
 
 
Property Class
The following class consists of three properties.
C#
public class Customer
{
    public int CustomerId { getset; }
    public string Name { getset; }
    public string Country { getset; }
}
 
VB.Net
Public Class Customer
    Public Property CustomerId As Integer
    Public Property Name As String
    Public Property Country As String
End Class
 
 
Populating Repeater using DataReader
Inside the Page Load event handler, the GetCustomers method is called and is used to populate the Repeater control.
Inside the GetCustomers method, the records from the Customers Table is fetched using SqlDataReader and then using WHILE Loop, the records are copied into the Generic List collection of Customer class objects.
Finally, the Generic List collection of Customer class object is returned.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        rptCustomers.DataSource = this.GetCustomers();
        rptCustomers.DataBind();
    }
}
 
private List<Customer> GetCustomers()
{
    string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
        {
            List<Customer> customers = new List<Customer>();
            cmd.CommandType = CommandType.Text;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(new Customer
                    {
                        CustomerId = Convert.ToInt32(sdr["CustomerId"]),
                        Name = sdr["Name"].ToString(),
                        Country = sdr["Country"].ToString()
                    });
                }
            }
            con.Close();
            return customers;
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        rptCustomers.DataSource = Me.GetCustomers()
        rptCustomers.DataBind()
    End If
End Sub
 
Private Function GetCustomers() As List(Of Customer)
    Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constring)
        Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
            Dim customers As List(Of Customer) = New List(Of Customer)()
            cmd.CommandType = CommandType.Text
            con.Open()
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                While sdr.Read()
                    customers.Add(New Customer With {
                        .CustomerId = Convert.ToInt32(sdr("CustomerId")),
                        .Name = sdr("Name").ToString(),
                        .Country = sdr("Country").ToString()
                    })
                End While
            End Using
            con.Close()
            Return customers
        End Using
    End Using
End Function
 
 
Screenshot
Bind (Populate) Repeater using DataReader in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads