Displaying the Newest Entries from a Database in VB
Setup
If you have not already done so, open Visual Studio and attach the database provided to you in the source files, located at the bottom of the tutorial.The Concept…
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. What we will do is simply have a repeater control and in the back end, have it select the top, say 5 from a table and order it by the ID and descending. Since ID’s are usually ascending, we can conclude that by making a list that is descending in ID’s will provide the latest database entries.Step one.
Our first step after attaching the database (or creating your own) is dragging a SQLDataSource control onto our default.aspx page in a pair of form tags and going into our code behind and adding the assembly reference presented below: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.
Save your changes to both files and switch back to your default.aspx page. We will add our repeater control and the ItemTemplate tags in order to have the data binding statements. The default.aspx code is as follows:
<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>
Using a SqlCommand in our code behind, we will select the 5 latest entries to the database. These 5 can be changed to 10, 20, etc. This statement will take place in the Page_Load method. We will also need to make sure as we open the connection to our database, we close and dispose the connection as well. The code for the default.aspx.vb page is as follows:
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim cmd As 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()
End Sub
End Class
Save and run your file. Note: You may encounter some issues if the code is copied directly and you implement your own database or additional coding, make sure that the database references and controls are linked and instantiated appropriately. You will notice that displayed on the end product are the latest entries of the table.
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
A Few Last Words…
Learning how to control and manipulate databases in ASP.NET is easy and we intend to be there every step of the way to help! Thank you for being a valued reader and join us next time for additional database tutorials!Display_NewestForm_Source_2010VB.zip
