Solved

populating combobox with datareader results

Posted on 2006-11-08
10
1,076 Views
Last Modified: 2009-12-16
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
0
Comment
Question by:stdcitunit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 39

Expert Comment

by:appari
ID: 17903874
try like this

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

0
 
LVL 10

Expert Comment

by:gangwisch
ID: 17904005
iterating through each record is not smart so try databinding

cmbTagYear.displaymember="fieldname"
cmbTagYear.valuemember="fieldname"
frm.cmbTagYear.DataSource=dr
0
 
LVL 2

Author Comment

by:stdcitunit
ID: 17904487
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???
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 12

Expert Comment

by:sandip132
ID: 17904857
first check whether your datareader has rows or not...
0
 
LVL 2

Author Comment

by:stdcitunit
ID: 17904978
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
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17905025
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
0
 
LVL 39

Expert Comment

by:appari
ID: 17905059
try using getstring instead of getvalue

like this

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

Author Comment

by:stdcitunit
ID: 17905253
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
0
 
LVL 39

Accepted Solution

by:
appari earned 50 total points
ID: 17905325
i think datareader object cannot be used to set to datasource property.
use datatable instead of datareader.

From MSDN: to use datasource trhe object must implement ILIst interface, datareader is not
http://msdn2.microsoft.com/en-us/library/system.windows.forms.combobox.datasource.aspx
Property Value
An object that implements the IList interface, such as a DataSet or an Array. The default is a null reference (Nothing in Visual Basic).
0
 
LVL 12

Expert Comment

by:sandip132
ID: 17911790
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.
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question