1
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

2
  • Actually, I dont understand, what do you want to do? be a specific pls Commented Jul 7, 2010 at 11:59
  • i have got newly added students courseId "newStudentCourseId" in string form, and i have a list box in which i want to set selected index of course to newly students added courseid Commented Jul 7, 2010 at 12:02

6 Answers 6

7
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;
Sign up to request clarification or add additional context in comments.

Comments

0

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 ?

Comments

0

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

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

Comments

0

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

1 Comment

find by value talkes string parameter so i converted it to string type wile i was getting it int type ...but i had reconverted it to int type by using convertToInt32 but of no avail whats wrong i am not getting but i have got solution to my problem by seeing above solution but can u tell whats wrong in this
0

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.

1 Comment

find by value talkes string parameter so i converted it to string type wile i was getting it int type ...but i had reconverted it to int type by using convertToInt32 but of no avail whats wrong i am not getting but i have got solution to my problem by seeing above solution but can u tell whats wrong in this
0

Surely you need;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.