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.
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
精彩评论