boo_boo_bear
boo_boo_bear

Reputation: 199

Combobox sender SelectionChanged Event C#

So this code worked perfectly fine for another ComboBox, but even though I changed everything in the code the way it should be it refuses to work now - Different ComboBox, different source but you know ignoring that it is the same situation.

The code that's in the ComboBox's Selectionchanged Event:

ComboBox comboBox = (ComboBox)sender;
string id = comboBox.SelectedValue.ToString();

It is throwing:

Object reference not set to an instance of an object.

On the line with the Selected Value.

Upvotes: 1

Views: 4957

Answers (1)

Damith
Damith

Reputation: 63105

try below

ComboBox comboBox = sender as ComboBox;
if(comboBox!=null  && comboBox.SelectedValue != null){ 
      string id = comboBox.SelectedValue.ToString();
}

need to check related other codes to find why SelectedValue is null.

Upvotes: 1

Related Questions