comboBox items show from database

jutiyi

Member
Joined
Feb 16, 2005
Messages
14
Programming Experience
1-3
i created a database in sql server.the database consists of tables.
the table has product field.
when i debug the vb.net program,i want the window form automatically load the combox box(the combox bpx consists of items that populated from that databases)

example the product field got 5 records:
shoes
clothes
food
bag
watches

then when i run the vb.net program,i want the user can select the 5 items from that comboBox.

i now only just begin learning the vb.net and sel server,so i dun't quite sure how to do it.

So,if anyone know,may u show the code to me?

Thanks you very much.
 
There are a couple ways of doing this, I feel the best way is using datasets. Look into datasets (you'll find tons of information out there on how they work, benefits, etc) to do what you need to. First you will need to create a connection the SQL Server, configure your data adapter and then generate a dataset (you can do this within the design interface).

Once you have done this select properties on the combo box and look for display member and datasource, you will need to select the appropriate information.

Last on form load or whenever you want the combo box to populate you need to fill the dataset.

i.e. dataAdapter(Name.Fill(DataSetName)
 
jutiyi said:
i created a database in sql server.the database consists of tables.
the table has product field.
when i debug the vb.net program,i want the window form automatically load the combox box(the combox bpx consists of items that populated from that databases)

example the product field got 5 records:
shoes
clothes
food
bag
watches

then when i run the vb.net program,i want the user can select the 5 items from that comboBox.

i now only just begin learning the vb.net and sel server,so i dun't quite sure how to do it.

So,if anyone know,may u show the code to me?

Thanks you very much.

assuming your table name:table1 and it has a column called products and in the column products has a data shoes,clothes,food,bag,watches...etc..
VB.NET:
'cn is a connection
cn.open
dim cm as new sqlcommand("select products from table1",cn)
dim dr as sqldatareader = cm.executereader
while dr.read
combobox1.items.add(dr(0).tostring)
end while
dr.close
cn.close
and in selecting an item in the combobox...use the selectedvaluechanged property of your combobox.
VB.NET:
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
ListBox1.Items.Add(ComboBox1.Text)
	End Sub
 
Back
Top