Sean P
Sean P

Reputation: 949

Binding results of a sql query to listbox

I am getting stuck on this problem. It seems simple but for some reason im having trouble.

Here is what I have of the following:

 Try
        cn = New OleDbConnection("Provider=microsoft.Jet.OLEDB.4.0;Data Source=G:\Sean\BMSBonder3_0.mdb;")
        cn.Open()
        str = "Select Distinct BonderIdentifier From [Session]"
        cmd = New OleDbCommand(str, cn)
        dr = cmd.ExecuteReader

        dr.Read()
        If dr.Item(0).ToString <> "" Then
            ListBox1.Items.Add(dr.Item(0))
        End If

        cn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

This works to get only one of the values. Actually the last. how can i get all of them?

Sorry for the newb question. Searching didnt help too much.

Upvotes: 0

Views: 1833

Answers (1)

SLaks
SLaks

Reputation: 887453

You need to use a While loop to repeatedly execute your code until dr.Read() returns False.
For example:

While dr.Read()
    If dr.Item(0).ToString <> "" Then
        ListBox1.Items.Add(dr.Item(0))
    End If
Wend

Upvotes: 2

Related Questions