Solved

populating combobox with datareader results

Posted on 2006-11-08
10
1,071 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET - Error - Object not set in Module 4 39
Modal Popup Extender control 1 38
HTML - Color not displaying correctly in EMAIL. 6 49
VB.NET 2008 Winforms Signing 13 32
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 …
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 Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

829 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