Welcome to Asp.net, vb.net, C#.net Resources Sign in | Join | Help

ADO.Net

How to Display Data using the .NET DataList Control, ASP.NET 2.0 and VB.NET
This tutorial will show you how to display data using the .NET DataList Control, ASP.NET 2.0 and VB.NET

The DataList control is an easy to use tool similiar to the Repeater control.

First, you will need to import the System.Data.SqlClient namespace.

The System.Data.SqlClient namespace contains the SqlCommand and SqlConnection classes that we need in order to connect to our database and to send an SQL command to it.

Imports System.Data.SqlClient


We'll put our code in the Page_Load() event.

When the Page_Load() event fires, a new SqlCommand object is instantiated with our connection string and our command.

Afterwards, we will attempt to connect using the Open() method of our cmd.Connection object. Once it is connected we will attempt to execute the command we specified earlier (in this example "SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES" in the Northwind db).

If all goes well, we will have the results of our SQL query assigned to the dlExample's DataSource property. Now all we have to do is call the DataBind() method of our dlExample to bind the data to the control. The data is now ready to be displayed.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

Try

Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES", New SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"))

cmd.Connection.Open()

dlExample.DataSource = cmd.ExecuteReader()

dlExample.DataBind()

cmd.Connection.Close()
cmd.Connection.Dispose()

Catch ex As Exception
lblStatus.Text = ex.Message
End Try

End Sub



We have to add a few tags on the front end of the .aspx page to place where we want the DataList control to display its bound data. Inside of the main DataList tag, we use the <HeaderTemplate> tag to specify the column titles for our repeated data and the <ItemTemplate> tag to specify what HTML tags can go around that repeated data (in this case, <td> tags surround each item). So, the front end .aspx page looks something like this::

<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> Employee Data Using the GridView Control:</td>
<td align="center" bgcolor="#FFFFFF">
<asp:GridView ID="gvwExample" runat="server" AutoGenerateColumns="False" CssClass="basix" >
<columns>
<asp:BoundField DataField="firstname" HeaderText="First Name" />
<asp:BoundField DataField="lastname" HeaderText="Last Name" />
<asp:BoundField DataField="hiredate" HeaderText="Date Hired" />
</columns>
</asp:GridView>
<asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>



The flow for the code behind page is as follows.

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) Handles Me.Load

Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 5 firstname,lastname,hiredate FROM EMPLOYEES", New SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"))

Try

cmd.Connection.Open()

dlExample.DataSource = cmd.ExecuteReader()

dlExample.DataBind()

cmd.Connection.Close()
cmd.Connection.Dispose()

Catch ex As Exception
lblStatus.Text = ex.Message
End Try

End Sub
End Class


Published Saturday, February 02, 2008 4:43 PM by admin

Filed under: , , , ,

Comments

No Comments
Anonymous comments are disabled
 
Powered by Community Server, by Telligent Systems