Link to home
Start Free TrialLog in
Avatar of stdcitunit
stdcitunitFlag for New Zealand

asked on

populating combobox with datareader results

Hi, this would be a real simple question to those who are familiar with .NET...

How can I populate a comboBox with datareader results from SQL CE 2 (in VB.net 1)?
What am I missing?
variable dr is he datareader object.

            While dr.Read()
                      frm.cmbTagYear.DataSource = dr.GetValue(0)
            End while

If I out put dr.getvalue(0) in msgbox, it is showing the single results.

thanks
Avatar of appari
appari
Flag of India image

try like this

While dr.Read()
                      frm.cmbTagYear.Items.Add ( dr.GetValue(0))
End while

Avatar of gangwisch
gangwisch

iterating through each record is not smart so try databinding

cmbTagYear.displaymember="fieldname"
cmbTagYear.valuemember="fieldname"
frm.cmbTagYear.DataSource=dr
Avatar of stdcitunit

ASKER

Hmmm, Tried both, the first one doesn't fill in the combobox with any values.
The second one comes up with "Unhandled exception" with no useful info to go with...

Ant idea???
first check whether your datareader has rows or not...
Just for the testing purpose, I am using a query that will return 3 values (rows). It can read in
While dr.Read()
msgbox (dr.GetValue(0))                    
End while
This outputs 3 times as expected.

thanks
In that case appari's code SHOULD work

>>
   While dr.Read()
      frm.cmbTagYear.Items.Add ( dr.GetValue(0))
   End while
<<

Where is your code?  What is frm?  If the code is in the same form as the combobox then you need just

      cmbTagYear.Items.Add ( dr.GetValue(0))

or

      Me.cmbTagYear.Items.Add ( dr.GetValue(0))

Roger
try using getstring instead of getvalue

like this

While dr.Read()
                      frm.cmbTagYear.Items.Add ( dr.GetString(0))
End while
Thanks Appari,
Your suggestion does work (very much appreciated):

While dr.Read()
                      frm.cmbTagYear.Items.Add ( dr.GetString(0))
End while

I tried this (suggested by gangwisch):
frm.cmbTagYear.DataSource=dr
cmbTagYear.displaymember="fieldname"
cmbTagYear.valuemember="fieldname"

This is certainly the way I prefer as this doesn't appear to require the code to step throup every single row in the column.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A little modification :

You can do it with an ASP.NET dropdown list. But for
a Windows.Forms combobox, the datasource has to be an object that
implements IList, and datareaders don't.