How do you list the fields of a database in a combobox?

Hi, I was wondering what would be the easiest way to list all the fields that are in a database with a combobox I'm using VB .net 2005.


Answer this question

How do you list the fields of a database in a combobox?

  • Channelzero

    get the fields in a dataset and bind it as datasource

    ComboBox1.DataSource = dsFields


  • Ljhopkins

    comboBox1.Items.Add(DatabaseField)
  • mnn2501

    I dont know what type of database you are working with but lets take the case of an sql server database. You can do this

    Imports System.Data.SqlClient

    sub foo()

    dim sql as string="select * from " & yourtablename 'set your query
    Dim conn as SqlConnection=New SqlConnection(yourConnectionString)
    conn.Open()
    Dim cmd as SqlCommand=New SqlCommand(sql,conn)
    Dim dr as SqlDataReader=cmd.ExecuteReader()

    Dim fdName as String

    ComboBox1.Items.Clear()

    for i as Integer=0 to dr.FieldCount-1

    ComboBox1.Items.Add(dr.GetName(i))

    next

    'load data into other controls

    dr.Close()

    conn.Close()

    'Dispose the Connection and datareader

    end sub

    NB: This is not the best approach but it works if you want to use your combo box in an unbound state (i.e. not bound to any database). The above will also work with OleDb such as Access too.

    Another is to query INFORMATION_SCHEMA like this

    sql="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='your tableName'"

    dim cmd as new sqlcommand(sql,conn) ; Please remember to declare and open a connection (i.e. conn) before you use it

    dim dr as sqlDataReader=cmd.executereader()

    combobox1.items.clear()

    while dr.Read()

    combobox1.items.add(dr.GetValue(0))

    end while

    dr.Close

    etc.

    Hope this will help you.


  • William Lowers

    The GetSchema methods allow you to return any db schema information including field names from a table....you can also use a data reader and pull column names from the reader or if you intend on using a dataset you can also get column names from the table in the dataset.

    pseudo code:

    Dim dt As New DataTable

    For Each c As DataColumn In dt.Columns

    Debug.Print(c.ColumnName)

    Next



  • Jackot

    I'm sorry, I should have been a little more specific. I'm using an Access database and I know the name of the table I'll need to pull the field names from. I don't need the data in the fields for this part of the program, I just need the list of the fields to populate the comobox so that they can pick and choose from one of the fields. The field names and the number of fields will always vary from database to database. I know what KAG posted is for SQL and not Access but when I have a chance later today I'll look closer at his code and see if I can get it to work.
  • SHMD

    All the fields in a database.

    I would assume you would be looking at fields in a specific table in a database.

    A database can contain many fields in many tables and views and displaying them all in a combobox would be rather nonsensical. You need to know which field is associated with which table.

    Hence being a bit more specific and showing the fields associated with a table in a database or using a control like a listview .datagridview to show more information on each field such as the associated table etc.


  • How do you list the fields of a database in a combobox?