fill combobox

karunam

Member
Joined
Aug 1, 2005
Messages
11
Programming Experience
Beginner
hi,
I am working with windowsapplication,vb.net using datareader object for select query.how can i assign displaymember and value member of combobox of datareader items.

say i have id,desc in table.
strsql = "Select skillid,skilldesc from skillmaster"
mycommand =
New SqlCommand(strsql, Conn)
Dim dr As SqlDataReader
'executing the command and assigning it to connection
dr = mycommand.ExecuteReader()

Do While dr.Read
cmbSkillId.Items.Add(dr(1).ToString())
Loop


but how can i give the displaymember and datamember for combo

'displaying the data from the table
dr.Close()
Conn.Close()


Thanks in advance
karuna
 
Hi, you can only use the datasource and display member properties if you are binding the combobox to a datasource i.e a datatable..
You are simply just reading the values in fro the datareader. If you want the combobox bound then you will have to fill a datatable with your information from your database then you can set the properties you want to bind it to the datasource.
 
Hi

Try to use the followin code:

cb.datasource=dataview
cb.displaymember=<Name of the column u want to display>
cb.valuemember=<Name of the column u want to set for value>
For Example
cb.datasource=dtCountry
cb.displaymember="CountryName"
cb.valuemember="CountryCode"
 
You can simply use a DataAdapter instead of a DataReader to Fill a DataTable directly, but if you want the additional efficiency of a DataReader then here's some code I wrote to populate a DataTable using a DataReader. I'm not sure whether it is still significantly more efficient than a DataAdapter mind you. Also, you other option would be to use a DataReader and create objects with the data and add them to a collection, like an ArrayList. You can then bind that collection to the control and set the names of two properties of your type to the DisplayMember and ValueMember.
 
Back
Top