Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 15th, 2008, 03:46 PM
Registered User
Join Date: Aug 2008
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Default Placing database items into a list box

I am attempting to fill a listbox with records from a database table and cannot seem to figure it out.
I understand how to use listbox.items.add(xxx)to place items in the listbox manually . I just cant seem to figure out how to add a datasource and automatically pull the list from a particular field in the table. I have tried and tried...maybe my head is just overloaded at the moment...I'm going to take a break for now...any help and an example would be greatly appreciated! thank you

The Following User Says Thank You to hookenbook For This Useful Post:
Old October 12th, 2008, 04:06 PM
Friend of Wrox
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts

I think you should take a look at the data binding properties of a listbox along with the data source and binder controls. And maybe some msdn articles to go with them.

Apocolypse2005, I'm a programmer - of sorts.
Old January 14th, 2009, 08:56 PM
Registered User
Join Date: Jan 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default An example of doing this programmatically

Here's an example of how to populate a list box with data from the Northwind database which comes with MS Access. Coincidentally this was an exercise in Theron Willis' Beginning Visual Basic 2005 Databases (Chapter2, Exercise 1). Thanks to Mr. Willis for this book, it has been a great help to me!

Imports System.Data
Imports System.Data.OleDb
PublicClass Form1
 PrivateSub Form1_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
      'Create a connection to the database
      Dim strConnection AsString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Office\Office11\Samples\Northwind.mdb;"
      Dim objConnection AsNew OleDbConnection(strConnection)
      'Open the connection with error handling
      Catch OleDbExceptionErr As OleDbException
      Catch InvalidOperationErr As InvalidOperationException
      'Create a command object with the SQL statement needed to select the first and last names
      Dim strSQL AsString = "SELECT FirstName, LastName FROM Employees"
      Dim objCommand AsNew OleDbCommand(strSQL, objConnection)
      'Create a data adapter and data table then fill the data table
      Dim objDataAdapter AsNew OleDbDataAdapter(objCommand)
      Dim objDataTable AsNew DataTable("Employees")
      'Create connection and release resources
      objConnection = Nothing
      objCommand = Nothing
      objDataAdapter = Nothing
      'Fill names into the listbox
      ForEach row As DataRow In objDataTable.Rows
           lstNames.Items.Add(row.Item("FirstName") & " " & row.Item("LastName"))
      'Release resources
      objDataTable = Nothing
What this example is doing is opening a connection to the Northwind database, selecting the data from the FristName and LastName columns of the Employees table, and filling that data into a data table. I then use a For Each Next loop to iterate through each row in the data table to add the value in the FirstName and LastName column to the listbox.


Similar Threads
Thread Thread Starter Forum Replies Last Post
put mailto: links on list items in a combo box? webwolf Flash (all versions) 0 September 12th, 2008 08:01 AM
adding items to list box using javascript k.manisha ASP.NET 1.0 and 1.1 Professional 3 February 16th, 2007 06:32 AM
Moving Selected Items from a list box to excel AlanAtMars SQL Server 2000 2 August 19th, 2005 02:02 PM
Choosing Multiple Items in a List Box HenryE Access 1 July 25th, 2005 07:14 AM
list box 2 show items according 2 current variable warrenb24 ADO.NET 0 December 20th, 2004 12:01 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.