Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

populating combobox with datareader results

Posted on 2006-11-08
10
Medium Priority
?
1,083 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

885 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