DotNet Tutorials

Server Intellect

 Displaying Data using ASP.NET 2.0 CheckBoxList and VB

This tutorial will show you how to display data using the .NET CheckBoxList Control, ASP.NET 2.0 and VB.NET

The .NET Framework offers a number of classes that makes populating controls with data easy.

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.

We will need to first import the System.Data.SqlClient namespace. The System.Data.SqlClient namespace contains the methods we will need to query our SQL database.

Imports System.Data.SqlClient

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

When the btnSubmit_Click() event fires it queries our database and creates a new SqlDataReader by invoking the ExecuteReader() method of our cmd object. We make sure to specify the DataTextField property so the CheckBoxList control will know which columns to display as a list.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.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()

Dim datareader As SqlDataReader = cmd.ExecuteReader()
chkBoxEx.DataSource = datareader
chkBoxEx.DataTextField = "firstname"

chkBoxEx.DataBind()

cmd.Connection.Close()
cmd.Connection.Dispose()
Catch ex As Exception
lblStatus.Text = ex.Message
End Try
End Sub

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

The front end .aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1" style="height: 62px"> Employee Data Populating A CheckBoxList Control:</td>
<td align="center" bgcolor="#FFFFFF" style="height: 62px">
<asp:CheckBoxList ID="chkBoxEx" runat="server">
</asp:CheckBoxList><asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>
</table>

The flow for the code behind page is as follows.

Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.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()

Dim datareader As SqlDataReader = cmd.ExecuteReader()
chkBoxEx.DataSource = datareader
chkBoxEx.DataTextField = "firstname"

chkBoxEx.DataBind()

cmd.Connection.Close()
cmd.Connection.Dispose()
Catch ex As Exception
lblStatus.Text = ex.Message
End Try
End Sub
End Class

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.



Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!
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!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect