Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleLoad a ListBox from a database query in VB .NET
Keywordsdatabase, VB.NET, ListBox, load
CategoriesVB.NET, Database, Controls
 
Execute the query. Use a data reader object to loop through the results, adding the fields' values to the ListBox separated by tab characters.
 
Private Sub LoadListBoxFromQuery(ByVal lst As ListBox, _
    ByVal query As String)
    lst.Items.Clear()

    ' Open the connection.
    connUsers.Open()

    ' Make a SELECT Command.
    Dim cmd As New OleDb.OleDbCommand( _
        query, connUsers)

    ' Execute the query.
    Dim db_reader As OleDbDataReader = _
        cmd.ExecuteReader(CommandBehavior.Default)

    ' Display the results.
    Dim txt As String
    Dim i As Integer
    Do While db_reader.Read
        txt = db_reader.Item(0).ToString
        For i = 1 To db_reader.FieldCount - 1
            txt &= vbTab & db_reader.Item(i).ToString
        Next i
        lst.Items.Add(txt)
    Loop

    ' Close the connection.
    connUsers.Close()
End Sub
 
For lots more information on database programming in VB .NET, see my book Visual Basic .NET Database Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated