简体   繁体   中英

Show information of database in a listbox vb.net

I want to show information of my database (ACCESS) and show that in a listbox.

I tried to do that with this follow code:

Public Sub listbox()

    Dim dt As DataTable

    Using con
        con.Open()
        Dim cmd As New OleDbCommand("SELECT * FROM Livrosescola", con)
        Dim oDA As New OleDbDataAdapter(cmd)
        dt = New DataTable()
        oDA.Fill(dt)
    End Using

    ListBox1.DataSource = dt

Didnt work :/ Can somebody help me?

You need to call the DataBind () method of the ListBox. After data link control in ASP.NET, you always have to call the DataBind () method.

 ListBox1.DataBind()

More info: Control.DataBind Method

Just needed to add "ListBox1.DisplayMember = "FieldNameYouWantToShow" and call "listbox()" in

Private Sub Livros_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\311026\Documents\PAP3\Biblio.accdb"
    con.Open()

listbox()
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM