?
Solved

Building an Array

Posted on 2004-11-09
2
Medium Priority
?
166 Views
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
0
Comment
Question by:mwalsh2000
2 Comments
 
LVL 3

Expert Comment

by:RacinRan
ID: 12538253
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
 
LVL 1

Accepted Solution

by:
jrcooperjr earned 200 total points
ID: 12538857
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

807 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