Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

populating combobox with datareader results

Posted on 2006-11-08
10
Medium Priority
?
1,078 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

705 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