stdcitunit
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
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
iterating through each record is not smart so try databinding
cmbTagYear.displaymember=" fieldname"
cmbTagYear.valuemember="fi eldname"
frm.cmbTagYear.DataSource= dr
cmbTagYear.displaymember="
cmbTagYear.valuemember="fi
frm.cmbTagYear.DataSource=
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???
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...
ASKER
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
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
>>
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
like this
While dr.Read()
frm.cmbTagYear.Items.Add ( dr.GetString(0))
End while
ASKER
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="fi eldname"
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
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=
cmbTagYear.displaymember="
cmbTagYear.valuemember="fi
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
While dr.Read()
frm.cmbTagYear.Items.Add ( dr.GetValue(0))
End while