Populate (Bind) Repeater using DataTable in ASP.Net using C# and VB.Net

Last Reply one month ago By Adnaan

Posted one month ago

 Hello,

how do I set the following code to pull it from the database?

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
    new DataColumn("Id"), new DataColumn("Lesson"), new DataColumn("StartTime"),
});
dt.Rows.Add(1, "Matematik", "08:28:00");
return dt;

Sama this; 

var dtilanGenel = klas.GetDataTable("SELECT Id,Lesson, StartTime");
rptLessons.DataSource = dtilanGenel;
rptLessons.DataBind();

Thanks for help.

You are viewing reply posted by: Adnaan one month ago.
Posted one month ago

Hi merictokatlio...,

you can refer below sample

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind database

HTML 

<form id="form1" runat="server">
    <div>
        <asp:Repeater ID="rptEmployees" runat="server">
            <HeaderTemplate>
                <table cellspacing="0" rules="all" border="1">
                    <tr>
                        <th scope="col" style="width: 80px">Name
                        </th>
                        <th scope="col" style="width: 100px">Date of Birth
                        </th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
                    </td>
                    <td>
                        <asp:Label ID="lblBirthDate" runat="server" Text='<%# Eval("BirthDate") %>' />
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
</form>

Namespaces

C# 

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net 

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Code

C# 

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindRepeater();
    }
}

private void BindRepeater()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT FirstName +''+ LastName AS 'Name', BirthDate FROM Employees", con))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                rptEmployees.DataSource = dt;
                rptEmployees.DataBind();
            }
        }
    }
}

VB.Net 

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindRepeater()
    End If
End Sub

Private Sub BindRepeater()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("SELECT FirstName +''+ LastName AS 'Name', BirthDate FROM Employees", con)
            Using sda As New SqlDataAdapter(cmd)
                Dim dt As New DataTable()
                sda.Fill(dt)
                rptEmployees.DataSource = dt
                rptEmployees.DataBind()
            End Using
        End Using
    End Using
End Sub

Screenshot