Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 426

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/templates/ja_purity/ja_templatetools.php on line 49
Bind an ADO.NET DataTable to a ListBox in VB.NET - The Scripts Library Community

Free Programming Tutorials & Source Code

 
  • Increase font size
  • Default font size
  • Decrease font size
Home Visual Basic.NET Bind an ADO.NET DataTable to a ListBox in VB.NET

Bind an ADO.NET DataTable to a ListBox in VB.NET

E-mail
(1 vote, average: 4.00 out of 5)
This example shows how to bind an ADO.NET DataTable to a ListBox in VB.NET and MS Access Database.
First, create a MS Access Database, and name it as db. Add an Employees table to this database:
Table name: Employees
Fields:
ID (number, primary key)
Name (text)

You can modify this Database to fit in your requirements.

Create a new windows project in Visual Studio.NET, and then add a form to this project.
Add a ListBox to the form, name is as lstEmployees.

bind listbox vb.net

Switch to the View Code Mode, and add the BindListBox sub:

    Sub BindListBox()
        Dim pk(0) As DataColumn
        Dim dt As New DataTable
        Dim strconn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Power\Desktop\db.mdb"
        Dim conn As OleDbConnection
        Dim da As OleDbDataAdapter
        Try
            conn = New OleDbConnection(strconn)
            da = New OleDbDataAdapter("Select * from Employees", conn)
            dt.Clear()
            da.Fill(dt)
            pk(0) = dt.Columns("ID")
            dt.PrimaryKey = pk
            conn.Close()
            Me.lstEmployees.DataSource = dt
            Me.lstEmployees.DisplayMember = "Name"
            Me.lstEmployees.ValueMember = "ID"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Call this sub from the Form Load Event:

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

That’s it!