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

Recommended Answers

All 5 Replies

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.

Actually i need one field to be loaded.

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)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.