Combobox ValueMember Value

Posted on 2011-04-22
Last Modified: 2012-05-11
I'm having a hard time figuring out how to reference the value of the ValueMember property of a combobox.  I'm using a dataset that I pull from a sql server; i'm able to get the text for the DisplayMember ok, but when I try to display the ValueMember it just gives me "fid".
How do I display the value associated with ValueMember?

        Dim conFrequencies As New SqlConnection(conStringFrequencies)
        Dim daFrequencies As New SqlDataAdapter(strSQLFrequencies, conFrequencies)
        Dim dsFrequencies As New DataSet
        daFrequencies.Fill(dsFrequencies, "Frequencies")

        With cboFrequencies
            .DisplayMember = "name"
            .ValueMember = "fid"
            .DataSource = dsFrequencies.Tables("Frequencies")
            .SelectedIndex = 0
        End With
Question by:vbNewbie2009
    LVL 8

    Expert Comment

    verify that that column exists.  Its most likely not in the Table as fid.
    LVL 33

    Expert Comment

    what is value of strSQLFrequencies?
    make sure you are selecting both fid and name.
    LVL 16

    Accepted Solution

    You cannot display ValueMember item but you can get value of selected item as cboFrequencies.SelectedValue

    if you want something like multivalue display then think about ListView or some other control.
    LVL 1

    Author Comment

    The value of strSQLFrequencies is:
    Dim strSQLFrequencies As String = "select fid, name from frequencies order by name"

    I really don't care to see multi value's in the control, just wanted to be able to show a sensible name to the user they could pick but use the id of the selection instead of the name.  Maybe this is the wrong approach?

    LVL 33

    Expert Comment

    what happen if you run the sql in mssql management studio? is name filled or empty?
    LVL 1

    Author Comment

    it is filled; just a simple table for now, but this is the results:

    fid      name
    1      10 DAY
    2      20 DAY
    4      MONTHLY
    3      PER FILL
    LVL 1

    Author Closing Comment

    Using cboFrequencies.SelectedValue worked!  Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now