Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i populate a listbox with a dataset

Status
Not open for further replies.

shaunieb

Programmer
Sep 2, 2004
26
ZA
hI, I'm mailny a asp.net guy and its pretty straight forward to do this with dropdownboxes etc is asp.net but for the life of me i'm striggling in vs to populate my listbox. I get System.Data.Datarowview instead of the values from my request. Please help. I've tried to do this for about 4 hrs and i'm sure im missimg something really small.
:)

Private Sub tables_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tables.SelectedIndexChanged
data = New DataTable
da = New MySqlDataAdapter("SELECT id FROM Company", conn)
cb = New MySqlCommandBuilder(da)

Dim ds As DataSet = New DataSet
da.Fill(ds, "Company")

Dim dv As DataView = ds.Tables("Company").DefaultView
ListBox1.DisplayMember = "Id"
ListBox1.DataSource = dv

End Sub
 
Hi not sure whether you've solved the problem but I had the same issue, I realised its a case error, Make sure the "Id" is in the same case in your dataset
 
Firstly, let me suggest you use a listview (just looks better than a listbox)

secondly, here's a snippet of code that connects to a DB and fills the listview ...


Dim sqlcnnReadOnly As New SqlConnection
Dim sqlcmndReadOnly As New SqlCommand
Dim dr As SqlDataReader

lvEmployees.Items.Clear()

sqlcnnReadOnly.ConnectionString = _
"data source = DATASOURCENAME;" & _
"integrated security = true;" & _
"initial catalog = COLLECTIONOFTABLES"
sqlcnnReadOnly.Open()

sqlcmndReadOnly.Connection = sqlcnnReadOnly
sqlcmndReadOnly.CommandText = _
"select * from tblEmployee order by strSurname"
sqlcmndReadOnly.CommandType = CommandType.Text

dr = sqlcmndReadOnly.ExecuteReader

Do While dr.Read
Dim EmployeeSurname() As String = New String() {dr("strSurname")}
Dim EmployeeFirstname() As String = New String() {dr("strForename")}
Dim EmployeeIntStatus() As String = New String() {dr("IntStatus")}
Dim EmployeeID() As String = New String() {dr("strEmpID")}

Dim count As Integer
For count = 0 To EmployeeSurname.Length - 1
Dim listItem As New ListViewItem(EmployeeSurname(count))
listItem.SubItems.Add(EmployeeFirstname(count))
listItem.SubItems.Add(EmployeeIntStatus(count))
listItem.SubItems.Add(EmployeeID(count))
lvEmployees.Items.Add(listItem)


Next
Me.Controls.Add(Me.lvEmployees)
Loop

dr.Close()
sqlcnnReadOnly.Close()



Hope this is some help.

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top