Hi,
Could somebody provide me with an example of how to display data (some text) in a combobox?
Thanks
Collin
Hi,
Could somebody provide me with an example of how to display data (some text) in a combobox?
Thanks
Collin
This is very simple...
try this..
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
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?
Thanks
Collin
yes
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....
hello !
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
Regards
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?
Thanks
Collin
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
msgbox(combobox.text)
or if this is not what you want please rephrase you question:)
Regards
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 ;)
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.