Solved

combo box and sqlclient

Posted on 2004-08-18
8
336 Views
Last Modified: 2011-09-20
Hi,
I use SQL DataReader to get data from sql then add to combo box:

cboTest.Items.Add("xxx")
With this way, I just can set the display member of the box, but cannot set the value inside. Anyone can help?

Another way: Set DataSource property of combo box to a table of dataset.

Could you tell me the faster way to write this ? Or another way?
Thanks, PN.
0
Comment
Question by:phuongnguyen
[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
  • 5
8 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11829489
try something like this

Dim MyList as new sortedList

Do while myReader.Read
     dim strItem as string
     dim strItemID as string
     strItem = myReader("yourItem").ToString
     strItemID = myReader("yourItemID").ToString
     idList.Add(strItemID, strItem)
Loop

Now, bind the sortedlist to the combobox

cbotest.DataSource = idList
cbotest.DisplayMember = idlist.value
cbotest.ValueMemeber = idlist.key
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11829891
sorry, this should be correct

Dim MyList as new sortedList

Do while myReader.Read
     dim strItem as string
     dim strItemID as string
     strItem = myReader("yourItem").ToString
     strItemID = myReader("yourItemID").ToString
     Mylist.Add(strItemID, strItem)
Loop

Now, bind the sortedlist to the combobox
cbotest.DataSource = Mylist
cbotest.DisplayMember = Mylist.value
cbotest.ValueMemeber = Mylistlist.key
0
 
LVL 7

Expert Comment

by:ramesh12
ID: 11831805
Cant we set the datasource of combo box to a SqlDataReader
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11838668
Not directly, only the way you tried or like my way
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11838675
Otherwise use a datatable or dataset they can directly be used as a datasource
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 100 total points
ID: 11842824
Forget my previous suggestion this is the way to go

        Dim myList As New ArrayList

        While myReader.Read()
            myPeople.Add(New DictionaryEntry(myReader("yourid"), myReader("youritem")))
        End While

        Me.ComboBox1.DataSource = myList
        Me.ComboBox1.DisplayMember = "Value"
        Me.ComboBox1.ValueMember = "Key"

to get the information from the combobox use

        Dim myThing As DictionaryEntry = Me.ComboBox1.SelectedItem
        MessageBox.Show(mything.Key & " - " & mything.Value)

0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

707 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