Navigator: Home - Display - Displaying Newest Entries From a Database- ASP.NET & C#

Displaying Newest Entries From a Database- ASP.NET & C#

This tutorial will show how to display the latest entries to a database, using a Repeater Control. C# version.

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Displaying the last entries to the database (the newest ones) is rather simple. We can order the select statement using a SQL statement, and just select the last 5, 10, 20, etc.
First, we need this line:

using System.Data.SqlClient;

Next, we create a Repeater control to display the data:

<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>ID: <%#DataBinder.Eval(Container.DataItem, "theID")%><br />
Name: <%#DataBinder.Eval(Container.DataItem, "theName") %><br />
City: <%#DataBinder.Eval(Container.DataItem, "theCity")%><hr width="50px" />
</ItemTemplate>
<FooterTemplate></td></FooterTemplate>
</asp:Repeater>
</div>
</form>

Then, using a SQL Statement, we select the last 5 entries to the database. This 5 can be changed to 10, 20, etc. Depending on the needs and database size.
The code-behind would look something like this:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT TOP 5 * FROM [tblOne] ORDER BY [tblOne].theID DESC", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
cmd.Connection.Open();

Repeater1.DataSource = cmd.ExecuteReader();
Repeater1.DataBind();

cmd.Connection.Close();
cmd.Connection.Dispose();
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!
411asp.net123aspxDotNetFreaksServer Intellect