Could somebody provide me with an example of how to display data (some text) in a combobox?



Recommended Answers

All 15 Replies

This is very simple...

try this..

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim da As New SqlDataAdapter("select * from student where id>3", db.con)
            Dim ds As New DataSet
            da.Fill(ds, "1")

            For i As Integer = 0 To ds.Tables("1").Rows.Count - 1

        Catch ex As Exception
            MsgBox("Error : " + ex.Message)
        End Try
    End Sub

You will need
1. SqlDataAdapter
2. Dataset

Now first define the dataadapter with command string and connection string.
Then fill the dataset using the dataadapter.
After that you can run a loop and add individual data into the combobox.

I've already got the connection to my database set up and working correctly. I'll go ahead and try the method you advised.

I've got one more question, would there be a way of displaying 3 separate fields (e.g. id, name, price) from ms access on a single line in the combobox?




Just change the code little bit.

For i As Integer = 0 To ds.Tables("1").Rows.Count - 1
                Me.ComboBox1.Items.Add(ds.Tables("1").Rows(i)(0)+" - "+ds.Tables("1").Rows(i)(1)+" - "+ds.Tables("1").Rows(i)(2))

Thank you very much for that :) I'll give it a try.

Don't forget to give reputation. And mark the thread as solved....

hello !
you can do this in this way also .

dim mycon as new sqlconnection("connectionstring")
dim da as new sqldataadapter("select * from table1",mycon)
dim dt as datatable
combobox1.datasource = dt
combobox.displaymember = "field name you want to show "
combobox.valuemember  ="field name you want to get as a value "

Hope this will helps you

Thanks for your suggestion, but I've laready tried kingsonprisonic's and it works just fine.

How would I be able to use the record I selected in the combobox to perform an action? For example, how would I program Button2 in order to display the selected data in a MsgBox?



Dear waqasaslammmmeo,

As he need to insert multiple field in one ComboBox , i think your code can not do that..... In your code he can only insert one field of a table. Right????

you want to show msgbox with the selected data of combo by clicking on the button ? if this is what you want then you can do like this


or if this is not what you want please rephrase you question:)


yes you are right in my code you can just show single field from database .

You're everywhere, Reverend Jim. Thanks :D

Dim mycon As New sqlconnection("Connestion String")

  •      mycon.open()
  • Dim da As New sqldataadapter("select * from invoice", mycon)

  • Dim dt As New DataTable

  • da.Fill(dt)

  • ComboBox1.DataSource = dt

  • ComboBox1.DisplayMember = "ComboBox1"

  • ComboBox1.ValueMember = " Data Base Colum Name"

  •  mycon.Close()

Hey I want to retrieve data from my access database in the textbox without using datagrid can you help me with this.
Thanks & Regards

commented: Avoid 9+ year old discussions to ask your new question. Most won't see this. Make a new discussion. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.