Search code examples
c#comboboxvpnvaluemember

Null value when retrieving value from combobox C#


This is supposed to get the value in a database attached via VPN. I have the query set to where it shows the title of the value in a dropdrown list and the system is supposed to get the valuemember of the selected value. Hence the valuemember in the field. It never finds a value, it just locks up. I know the VPN is active to access the database. Other than that I see no reason for the valuemember not to be transferring to the labelcode variable.

int labelcode = Convert.ToInt32(cmbboxLabel.ValueMember);

Solution

  • try this

    int labelcode = Convert.ToInt32(cmbboxLabel.SelectedValue);

    ValueMember is a string that tells the ComboBox which attribute of the bound object to give back with SelectedValue.

    If your underlying class looked like this:

    public class Example
    {
       public string Title {get; set;}
       public int Value {get; set;}
    }
    

    Then you would set combobox.DisplayMember to "Title" and combobox.ValueMember to "Value".