I have a dropdownlist on a webform, which is populated from a DataSet. i have a string variable, and i want this to be the item selected on the dropdownlist.
the following works fine and selects the item i want it to:
the following returns no error but doesnt work, ie the first item in the list is selected:
...and the following throws a NullReferenceException "object reference not set to an instance of an object":
I dont understand - the dropdownlist is populated from a DataSet and is databound as it should be. the myString value is definitely in the returned records. For some reason myDropdown.Items.FindByText(myString) is null, which explains why the second and third lines of code above dont work/throw an error.
I'm clearly missing something obvious, but cant for the life of me guess what it might be.
If anyone can spot what's going on, I would very much appreciate it!
thanks in advance
the following works fine and selects the item i want it to:
Code:
myDropdown.SelectedIndex = myDropdown.Items.IndexOf(myDropdown.Items[213]));
the following returns no error but doesnt work, ie the first item in the list is selected:
Code:
myDropdown.SelectedIndex = myDropdown.Items.IndexOf(myDropdown.Items.FindByText(myString));
...and the following throws a NullReferenceException "object reference not set to an instance of an object":
Code:
myDropdown.Items.FindByText(myString).Selected = true;
I dont understand - the dropdownlist is populated from a DataSet and is databound as it should be. the myString value is definitely in the returned records. For some reason myDropdown.Items.FindByText(myString) is null, which explains why the second and third lines of code above dont work/throw an error.
I'm clearly missing something obvious, but cant for the life of me guess what it might be.
If anyone can spot what's going on, I would very much appreciate it!
thanks in advance