Building an Array

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
mwalsh2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RacinRanCommented:
A direct loop example direct from the help file:

    ' Shutdown the painting of the ListBox as items are added.
    listBox1.BeginUpdate()
    ' 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.
    listBox1.EndUpdate()

or you can databind

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


Racin
0
jrcooperjrCommented:
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)
    myConnection.Open()
    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
      myReader.Close()
'''' finish clean up by closing the connection
    myConnection.Close()
End Sub


 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.