Display record from Database in ASP.Net Repeater horizontally using C# and VB.Net

Last Reply 3 months ago By pandeyism

Posted 3 months ago

I wanted to show the latest news horizontally like below image using Repeater.

Last time i displayed the news vertically by using datalist.

can we display the latest news horizontally by using Repeater?

if yes, can anyone provide me the sample code?

Posted 3 months ago Modified on 3 months ago

Hey chetan,

As you are looking for showing data in horizontally so for that use DataList, it's not possible in Repeater.

And getting latest date of news so use OrderBy in your query with column name which column you want to sort.

Database

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

Download Northwind Database

HTML

<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="3">
    <ItemTemplate>
        <div style="border: 2px solid #ccc;">
            <b>Last Name:</b>
            <asp:Label ID="Label1" Text='<%# Eval("LastName") %>' runat="server" />
            <br />
            <b>First Name:</b>
            <asp:Label ID="Label2" Text='<%# Eval("FirstName") %>' runat="server" />
            <br />
            <b>Birth Date:</b>
            <asp:Label Text='<%#Eval("BirthDate","{0:dd/MM/yyyy}") %>' runat="server" />
            <br />
        </div>
    </ItemTemplate>
</asp:DataList>

Namespaces

C#

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

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string strconstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strconstr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT LastName, FirstName,BirthDate FROM Employees ORDER BY BirthDate DESC", con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        da.Fill(dt);                       
                        DataList1.DataSource = dt;
                        DataList1.DataBind();
                    }
                }
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim strconstr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(strconstr)
            Using cmd As SqlCommand = New SqlCommand("SELECT LastName, FirstName,BirthDate FROM Employees ORDER BY BirthDate DESC", con)
                Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Using dt As DataTable = New DataTable()
                        da.Fill(dt)                       
                        DataList1.DataSource = dt
                        DataList1.DataBind()
                    End Using
                End Using
            End Using
        End Using
    End If
End Sub

Screenshot