views:

62

answers:

2

I have a multi-column combobox where the datasource is a List<> in my Select Class

Select selection = new Select();  
RadComboBox1.DataSource =  selection.GetAcctUtilCo(e.Text, 10).Skip(e.NumberOfItems); 

I have a few DataTextFields. My DataValueField is the AcctID.
Once an account is selected, I need the datatextfield values to populate some fields in a gridview.

I am trying to use the .Find() method by AcctID to retreive the data without success:(

int AcctID = Convert.ToInt32(RadComboBox1.SelectedValue); // *
List<Select> mylist = RadComboBox1.DataSource as List<Select>;  
mylist.Find(delegate(SelectTop act) { return act.AcctID == acctID; }); // ** exception here
Label lblAcctNo = (Label)grdAccts.HeaderRow.FindControl("lblAcctNo");  
lblAcctNo.Text = mylist.AccountNum;

When I debug, I get 'Object reference not set to the instance of an object' on the indicated line. AcctID is NOT null when I hover over it. However when I hover over mylist, it says null. I'm new with the .Find method & I'm really not sure if the problem is with that or with using the datasource of the combobox as mylist source.
Can someone please help enlighten me

+1  A: 

The problem is caused by RadComboBox1.DataSource - it is not persisted between page requests.

  1. In your code that sets RadComboBox1.DataSource, save a copy of the RadComboBox1.DataSource value in the viewstate. eg. ViewState["RadComboBox1"] = RadComboBox1.DataSource;

  2. In the event that runs your above code, restore the RadComboBox1.DataSource by reading the value from the viewstate. eg. RadComboBox1.DataSource = ViewState["RadComboBox1"];

You should hopefully find the values then persist between requests. Good luck!

Will
It's still not working. now I have: RadComboBox1.DataSource = selection.GetAcctUtilCo(e.Text, 10).Skip(e.NumberOfItems); ViewState["RadComboBox1"] = RadComboBox1.DataSource; RadComboBox1.DataBind(); Then, in the SelectedIndexChanged event, I have: int pkAcct = Convert.ToInt32(RadComboBox1.SelectedValue); List<Select> list = (List<Select>)ViewState["RadComboBox1"]; list is STILL null:( for the record, is storing in ViewState equivalent to storing it in a variable?
sher1000
+2  A: 

Your combo-box's data source is not a list. When you use "as" the result is null if you try to cast to an invalid type, instead of throwing an exception like a standard cast. Since you used the Skip function to create your data source you actually have an "IEnumerable<>".

Chris
I also tried without 'as' like this: List<Select> list = (List<Select>)(RadComboBox1.DataSource);
sher1000
@sher1000, the point is that *it's not a list*. Try 1 of 2 things. 1) Call `.ToList();` after `.Skip(...)` when you set the data source. 2) Use `IEnumerable<Select>` in place of `List<Select>` when you are creating `mylist`.
Anthony Pegram
Also, since you have extension methods, you have 3.5 at your disposal. You can simplify the Find method to be something like `.Find(act => act.AcctID == acctID);`
Anthony Pegram
I see what you mean. I changed it to: RadComboBox1.DataSource = selection.GetAcctUtilCo(e.Text, 10).Skip(e.NumberOfItems).ToList(); but when i hover over the RadComboBox1.DataSource - it STILL says 'null'. I'm new to .NET, so please bear with me
sher1000
anyone?????????
sher1000
let me rephrase my original question now that I know what I need:How would one assign a new List<> to the same List that populated a combobox.reason I want this is because why should I query the db again if I have the values right there?
sher1000
You really have two choices: either save the list in your session and get it back from there, or save it in your View State. Saving it in Session increases the memory requirements of your app per session, while view state will bloat your page and expose your data to the client.
Chris