NoviceToDotNet
NoviceToDotNet

Reputation: 10805

asp.net problem

selectCourseList.SelectedIndex=Convert.ToInt32(selectCourseList.Items.FindByValue(newStudentCourseId));

giving following error Unable to cast object of type 'System.Web.UI.WebControls.ListItem' to type 'System.IConvertible'.

i don't understand whats wwrong with this

Upvotes: 1

Views: 147

Answers (6)

fritterfatboy
fritterfatboy

Reputation: 337

Surely you need;

selectCourseList.SelectedIndex=selectCourseList.IndexOf(selectCourseList.Items.FindByValue(newStudentCourseId));

Upvotes: 0

tanathos
tanathos

Reputation: 5606

I think something like (not tested):

selectCourseList.SelectedIndex=-1;
selectCourseList.Items.FindByValue(newStudentCourseId).Selected = true;

where newStudentCourseId is a string representig the ItemValue of your DropDown.

Upvotes: 0

ma7moud
ma7moud

Reputation: 423

Are you sur that the item value is a int type? Are you sur that the method FindByValue dont return a null for your parameter newStudentCourseId

Upvotes: 0

David
David

Reputation: 218827

You can't convert a ListItem to an int. Try:

selectCourseList.SelectedIndex=selectCourseList.Items.IndexOf(selectCourseList.Items.FindByValue(newStudentCourseId));

Upvotes: 0

Martin Clarke
Martin Clarke

Reputation: 5657

selectCourseList.Items.FindByValue(newStudentCourseId)

The above is going to give you a ListItem back, so you can't use Convert.ToInt32 and then set the index the way you're describing.

Instead try:

selectCourseList.Items.FindByValue(newStudentCourseId).Selected = true;

Upvotes: 7

barrylloyd
barrylloyd

Reputation: 1589

selectCourseList.Items.FindByValue(newStudentCourseId) is returning some object that does not support the System.IConvertible interface - so the Convert.ToInt32 call is failing

What type of objects are in selectCourseList.Items ?

Upvotes: 0

Related Questions