Hi all,
I wanted to load a listbox with items from the database.I just wrote the code using oop.
But on the interface i'm not sure how to read and insert the items in the listbox.
How can i access the items being read at the client side(interface).

Public Sub LoadWorkItem()
        ' Load the data.
        ' Select records.
        Dim conn As New OleDbConnection
        Dim data_reader As OleDbDataReader
        conn = GetDbConnection()
        Dim cmd As New OleDbCommand("SELECT * FROM work_item ORDER BY [work item number]", conn)
        data_reader = cmd.ExecuteReader()
        If data_reader.HasRows = True Then
            Do While data_reader.Read()
                WorkItemNumber = data_reader.Item("work item number")
                Description = data_reader.Item("description")
            Loop
        End If
        data_reader.Close()
        data_reader = Nothing
        cmd.Dispose()
        cmd = Nothing
        conn.Close()
        conn.Dispose()
    End Sub

I suggest using a listview in details mode rather than a listbox. There are examples using ADO, SqlClient and OleDb here. Multicolumn listboxes are a pain.

Edited 3 Years Ago by Reverend Jim

I assumed you were adding WorkIitemNumber and Description as separtate items. To add one item you can do

ListBox1.Items.Add(string value)

perhaps you are looking for

ListBox1.Items.Add(WorkItemNumber & ": " & Description)

Edited 3 Years Ago by Reverend Jim

I didn't get the result using the above code you gave me.BTW i coded WorkItemNumber and Description using Get and Set method.Do they return the items in the database to the listbox?

 Dim con As New OleDbConnection
            con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = Fournisseur.accdb"
            con.Open()
            Dim cmd As New OleDbCommand
            cmd.Connection = con
            cmd.CommandText = "select Sectionname from Employeesection "
            Dim dbDR As OleDb.OleDbDataReader = cmd.ExecuteReader
            Do While dbDR.Read()
                listbox1.Items.Add(dbDR("Sectionname"))
            Loop
            con.Close()

this the right one

This article has been dead for over six months. Start a new discussion instead.