Results 1 to 2 of 2

Thread: Adding items from a database to a ComboBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Location
    NH USA
    Posts
    3

    Adding items from a database to a ComboBox

    I have a VB.NET program with an Access database that I am using. I want to have each item in a ComboBox taken from each row in a column in the database. Nothing fancy, yet I can't find any sample code anywhere that does this.

    This is driving me nuts, can anyone help me?

  2. #2
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    Code:
    Imports System.Data.oledb
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim myConnection As OleDbConnection
            Dim myDataAdapter As OleDbDataAdapter
            Dim myDataSet As DataSet
            Dim strConnection As String
            Dim strSQL As String
            Dim count As Integer
            Try
    
                strConnection = "Provider=Microsoft.jet.oledb.4.0;Data source=c:\names.mdb"
                strSQL = "select * from tblNames"
                myConnection = New OleDbConnection(strConnection)
    
                myDataAdapter = New OleDbDataAdapter(strSQL, myConnection)
                myDataSet = New DataSet
                myDataAdapter.Fill(myDataSet, "Names")
    
                Dim item As DataRow
                For count = 0 To myDataSet.Tables("Names").Rows.Count - 1
                    ComboBox1.Items.Add(myDataSet.Tables("names").Rows(count).Item(0))
                Next
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
    End Class
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width