Building an Array

Posted on 2004-11-09
Last Modified: 2010-04-23
Hello, I am running a SQL query which returns multiple values.  I would like to populate a list box with those values once the window is opened.  What is the best method of looping through the values returned in the query and and having them display in the list box?  Any help would be greatly appreciated.  Thank you
Question by:mwalsh2000
    LVL 3

    Expert Comment

    A direct loop example direct from the help file:

        ' Shutdown the painting of the ListBox as items are added.
        ' Loop through and add 50 items to the ListBox.
        Dim x As Integer
        For x = 1 To 50
            listBox1.Items.Add("Item " & x.ToString())
        Next x
        ' Allow the ListBox to repaint and display the new items.

    or you can databind

            ListBox1.DataSource = MyDataTable
            ListBox1.DisplayMember = "MyDisplayColumn"
            ListBox1.ValueMember = "MyPrimaryKeyValue"

    LVL 1

    Accepted Solution

    Hi.... I've used the following type of construction to get data from an SQL database and display it in a listbox...  this will fill the list box with an item for every row returned from the query.....
    (this is also in the .net help documentation)

    Public Sub ReadMyData(myConnString As String)
    ''''' build your query
        Dim mySelectQuery As String = "SELECT field1,field2 from table1"
        Dim myConnection As New SqlConnection(myConnString)
        Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
        Dim myReader As SqlDataReader
        myReader = myCommand.ExecuteReader()
          While myReader.Read()
    ''''''''''''' note that the read operation returns the items in your query.. item(0) is the first field, item(1) the second, and so on..)
    ''''''''''''''''''''in this example, there were only two fields
                 ListBox1.Items.Add( "Field 1 was " + myReader.Item(0) + "  Field2 was " + myReader.Item(1))
      End While
    '''' clean up by closing the reader
    '''' finish clean up by closing the connection
    End Sub


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now