Tutorial RSS
 
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.

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

Looking for more ASP.NET Tutorials? Click Here!

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.



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();
}
}



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

Looking for more ASP.NET Tutorials? Click Here!

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.


Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!

411asp.net123aspxDotNetFreaksServer Intellect