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 Combobox 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 Combobox in VB.NET

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

E-mail
(2 votes, average: 4.50 out of 5)
This example will show you how to bind datatable to a combobox in VB.NET
Fist of all, create an MS Access database, and name it as db. Add a new table to it:

Table name: Positions
Fields:
ID (number, primary key)
Position (text)

Create a new vb.net windows project; add a new form to it.
Add a combobox to the form, name is as cbPositions.

bind combobox

Switch to View Code mode, copy and paste the following sub:

    Sub FillComboBox()
        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 positions", conn)
            dt.Clear()
            da.Fill(dt)
            pk(0) = dt.Columns("ID")
            dt.PrimaryKey = pk
            conn.Close()
            Me.cbPositions.DataSource = dt
            Me.cbPositions.DisplayMember = "position" ' display the position name in the combo
            Me.cbPositions.ValueMember = "ID" ' set the ID to be the value of each member
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

You can call this sub in the Form Load Event:

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

You may see the same exampme in C#, Bind an ADO.NET DataTable to a Combobox in C#
That’s it!