Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am having a little difficulty sending the value from a listbox to an object.The lboxStudentIDs contains 4 values.
1111
2222
3333
4444

When I run the debugger I see that the "SelectedValue" is correct BUT aStudent remains null. The values in the listbox are integers. Any help is appreciated.
Thanks!

SchoolIDS aStudent  = lboxStudentIDs.SelectedValue as SchoolIDS;
Posted
Updated 11-May-11 14:56pm
v2
Comments
Wonde Tadesse 11-May-11 20:27pm    
Can you elaborate how you populate the list box ?.
WhiskeyBusiness 11-May-11 20:33pm    
Hi Wonde,
The listbox is databound. So lbboxStudentIDs.DataSource = something;
Wonde Tadesse 11-May-11 21:02pm    
Quick qn.Fill in the blank.
1. Are you talking about Asp.Net ? If so

ListBox1.DataSource = "blank";
ListBox1.DataTextField = "blank";
ListBox1.DataValueField = "blank";

2 Window Application ? If so,

listBox1.DataSource = "blank";
listBox1.DisplayMember="blank";
listBox1.ValueMember="blank";
WhiskeyBusiness 11-May-11 21:18pm    
Hi Wonde,

It is a Window Application

So
listBox1.DataSource = something;
something is a list.
listBox1.DisplayMember="StudentName";
listBox1.ValueMember="StudentID";
Wonde Tadesse 11-May-11 22:33pm    
What is "something" in your case. Is it SchoolIDS or what ?. If I don't know that it's difficult to answer.

1 solution

Assuming ASP.NET...

SelectedValue is of type string. Strings do not store instances of type StudentIDS. I'm guessing you are storing strings of the ID's (e.g., "1111"). Convert that SelectedValue to an integer, then use that integer to look up your student wherever you are storing students (I'm guessing in a database).

FYI, if the type of instance you are attempting to cast using the "as" keyword does not match the type you are trying to cast to, null will be returned. That's why you are seeing null (because strings aren't of type StudentIDS).
 
Share this answer
 
Comments
WhiskeyBusiness 11-May-11 20:46pm    
Hi AspDotNetDev,
The value of the ID's is an integer. So would I not cast as SchoolIDs to convert to the appropriate type ?
AspDotNetDev 11-May-11 21:16pm    
No, it is a serialized int, you need to parse it. Use int.Parse().
AspDotNetDev 11-May-11 21:22pm    
I see you added comments above that indicate your data source is a list. Once you have parsed your ID, loop through your list to find the student that has that ID.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900