2

I'm setting the value member and display member from a datareader to combobox like this.

public void getPartyNamesCombo()
{
    SqlDataReader reader = new VotingOP().getPartyNamesToCombo();
    while (reader.Read())
    {
        cmbPartyName.Items.Add(new { PartyID = reader["partyID"].ToString(), PartyName = reader["partyName"].ToString() });
    }
    cmbPartyName.ValueMember = "PartyID";
    cmbPartyName.DisplayMember = "PartyName";
}

I'm trying to access the id like this

int selectedValue = (int)cmbPartyName.SelectedValue;
MessageBox.Show("Selected value is"+selectedValue);

but it gives me "An unhandled exception of type 'System.NullReferenceException'" Exception. What's wrong I'm doing here?

DanM7
  • 2,203
  • 3
  • 28
  • 46
ChathurawinD
  • 756
  • 1
  • 13
  • 35
  • Even if the selected value wasn't null and you didn't get the `NullReferenceException` it still wouldn't work because your `SelectedValue` is a `string` and you're trying to cast it into an `int`. – t3chb0t Dec 29 '14 at 17:07

2 Answers2

2

I suggest the following approach:

First: you create some class for your data items:

class MyDataItem 
{
    public string PartyID { get;set; }
    public string PartyName { get;set; }
}

Second: you use it in place of your anonymous object:

public void getPartyNamesCombo()
{
    SqlDataReader reader = new VotingOP().getPartyNamesToCombo();
    while (reader.Read())
    {
        cmbPartyName.Items.Add(new MyDataItem() { 
            PartyID = reader["partyID"].ToString(), 
            PartyName = reader["partyName"].ToString() 
        });
    }
    cmbPartyName.ValueMember = "PartyID";
    cmbPartyName.DisplayMember = "PartyName";
}

Third: finally you are now able to cast your selected item to the custom data item and get its properties:

MyDataItem selectedItem = cmbPartyName.SelectedItem as MyDataItem;
if (selectedItem != null) 
{
    MessageBox.Show(String.Format("You've just selected the '{0}' party with the ID {1}",  selectedItem.PartyName, selectedItem.PartyID));
}

Notice here that I used the SelecetedItem which gives you the whole object unlike the SelectedValue where you only get the PartyID.

If you later change your mind and want to show other properties they will already be available.

t3chb0t
  • 16,340
  • 13
  • 78
  • 118
1

If there is no selected value currently then the cmbPartyName.SelectedValue will return null. First you need to get the selected value and check if it is not null:

object selectedValue = cmbPartyName.SelectedValue;
if (selectedValue != null)
{
    // Now convert the selected value to integer. 
    int selectedPartyID = (int)selectedValue;

    // And now you can handle the integer.
    // ...
}
else
{
    // There is no value selected... 
}
msporek
  • 1,187
  • 8
  • 21
  • Tried your code. Still items display in the combo box, though I select an item it displays no value selected. How can I solve that? – ChathurawinD Dec 29 '14 at 16:49
  • So just debug it line by line with F10 and you will see what is the problem. The solution above deals with the NullReferenceException as you asked to. – msporek Dec 29 '14 at 16:50