Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


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


ASP.NET Database Tutorial

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

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;

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

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>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

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!





 
  Developer Resources







Server Intellect Rocks