ARTICLE

Displaying Data in a ListBox Web Control

Posted by Praveen Kumar Articles | Visual Basic 2010 November 16, 2004
This article decribes how to display data in a listbox web control.
 
Reader Level:

This simple sample example shows you how to show a column dable in a ListBox control.  I 've used norhtwind.mdb Access 2000 database comes with Office 2000.

ListBox is generally used to display one or multiple columns. In this sample example, I'd show the contents of  "FirstName" column of  "Employees" table.

Same well known steps:

1. Create OleDbDataAdapter.

Private da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Employees", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb")

2. Create and Fill DataSet.

' Create a DataSet Object
Private ds As DataSet = New DataSet.
' Fill DataSet with the data
da.Fill(ds, "Employees")

3. Connect DataSet to the ListBox and call Page.DataBond().

' Set DataSource property of ListBox as DataSet's DefaultView
Private
ListBox1.DataSource = ds.Tables("Employees").DefaultView
Private
ListBox1.SelectedIndex = 0
' Set Field Name you want to get data from
Private
ListBox1.DataTextField = "FirstName"
' Bind the data
Page.DataBind()

Sample Code:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

' Put user code to initialize the page here
' Create an object of OleDbDataAdapter
Dim da As OleDbDataAdapter = New
OleDbDataAdapter("Select * from Employees", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb")
' Create a DataSet Object
Dim ds As DataSet = New
DataSet
' Fill DataSet with the data
da.Fill(ds, "Employees")
' Set DataSource property of ListBox as DataSet's DefaultView
ListBox1.DataSource = ds.Tables("Employees").DefaultView
ListBox1.SelectedIndex = 0
' Set Field Name you want to get data from
ListBox1.DataTextField = "FirstName"
' Bind the data
Page.DataBind()
End Sub

share this article :
post comment
 

Imports System.Data.OleDb Public Class Form1 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' Put user code to initialize the page here ' Create an object of OleDbDataAdapter Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Employees", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Administrator\Music\Northwind.mdb") ' Create a DataSet Object Dim ds As DataSet = New DataSet ' Fill DataSet with the data da.Fill(ds, "Employees") ' Set DataSource property of ListBox as DataSet's DefaultView ListBox1.DataSource = ds.Tables("Employees").DefaultView ListBox1.SelectedIndex = 0 ' Set Field Name you want to get data from ListBox1.DisplayMember = "FirstName" End Sub

Posted by khavy xxxxxxxx Dec 20, 2010

I need to know how to display a multiplication equations in a listbox going from teh numbers 1 to 15 and being capable of multiplying the number from 1 to 12.  For example hte results will show all the tables for 5 like 5*1=1 and so on in the listbox

Posted by AUDREY Dec 10, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
MOST LIKED ARTICLE
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor