Could somebody provide me with an example of how to display data (some text) in a combobox?
This is very simple...
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ComboBox1.Items.Clear() Try db.con.Open() 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 Me.ComboBox1.Items.Add(ds.Tables("1").Rows(i)(0)) Next Catch ex As Exception MsgBox("Error : " + ex.Message) Finally db.con.Close() End Try End Sub
You will need
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)) Next
Thank you very much for that :) I'll give it a try.
Don't forget to give reputation. And mark the thread as solved....
you can do this in this way also .
dim mycon as new sqlconnection("connectionstring") mycon.open() dim da as new sqldataadapter("select * from table1",mycon) dim dt as datatable da.fill(dt) combobox1.datasource = dt combobox.displaymember = "field name you want to show " combobox.valuemember ="field name you want to get as a value " mycon.close
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?
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 .
Also, in lines 7 & 8, "combobox" should be "combobox1"
You're everywhere, Reverend Jim. Thanks :D
Just pretty decent at proofreading ;)