Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

very annoying: FindByText

Status
Not open for further replies.

LFCfan

Programmer
Nov 29, 2002
3,015
GB
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:
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
 
OK, panic over - it was indeed something obvious: the myString needed trimming to match the dropdownlist entry

cant believe how long i spent puzzling over this before it dawned on me........
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top