This tutorial will show you how to display data using the .NET CheckBoxList Control, ASP.NET 2.0 and VB.NET
Looking for the C#.NET 2005 Version? Click Here!
Looking for more DB Tutorials? Click Here!
The .NET Framework offers a number of classes that makes populating controls with data easy. 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.
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
| 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 |
The front end .aspx page looks something like this:
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
<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 |
Looking for the C#.NET 2005 Version? Click Here!
Looking for more DB Tutorials? Click Here!
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.
|