Solved

combo box and sqlclient

Posted on 2004-08-18
8
330 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now