When I run this code my listbox
is empty. What is the best way to get data from SQL and into a listbox
? When the form is submitted I want to use the CustomreID
value data to store into another table and thought using the index would be the best solution.
sSQL = "SELECT CustomerID, Company from Customers Order by Company ASC"
cmd = New SqlCommand(sSQL, moConn)
rs = cmd.ExecuteReader()
While rs.Read
lsbDestination.Items.Insert(CInt(rs("CustomerID")), rs("Company"))
End While
You can easily bind data to a ListBox using the
DataSource
property of the ListBox. Try something like this (untested):Good luck.
If you want to add a items to
ListBox
usingWhile rs.Read
.Just let me know if you've encountered some problem.