This is a migrated thread and some comments may be shown as answers.

Selection out of range Parameter name error, need to be able to set values that don't exist in the ComboBox

13 Answers 1461 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 16 Oct 2008, 02:27 AM
Hi All

I'm getting the following error:
"Selection out of range Parameter name:"

I know what is causing the error - I'm using simple data binding, and the value I'm binding to the ComboBox doesn't exist in its list of values.  I'm looking for a way to work around this, because it's always possible that someone has changed a value in the database to one that doesn't exist in the ComboBox.

I've read that setting AllowCustomText="true" will allow values to be entered that aren't currently in the list, however it still seems to crash if the value I'm binding doesn't exist in the datasource.

Ideally, the functionality I want is:
The list will auto-populate from the datasource.
If the value I bind to the ComboBox doesn't exist in the datasource, then that value is automatically added to ComboBox and can be selected like any other value.
The users can only select values from the list, I wouldn't want them to be able to enter any value they like (which is what I think AllowCustomText does).

Is there any way to do this?

13 Answers, 1 is accepted

Sort by
0
Rosi
Telerik team
answered on 16 Oct 2008, 09:05 AM
Hello James,

The AllowCustomText property allows users to type text in the RadComboBox's input. It will not add an item with this text or with this value.

If you want users to select just from existing items, please make sure that the AllowCustomText and EnableLoadOnDemand properties are not enabled(they are false by default).

If the problem still persists, I suggest you call the ClearSelection() method immediately before calling the DataBind() method.


Kind regards,
Rosi
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
James
Top achievements
Rank 1
answered on 16 Oct 2008, 09:16 AM
Hi Rosi

That's not quite the solution I'm looking for.  Let me explain what I need with a simple example:

Lets say I have a combo box with these entries available to select from:
Bob
Chuck
Dan
Karen

We store the selected choice in a table, and if the user comes back to view the record, we auto populate the list with the value that was chosen before.  So time goes on, and the client decides that Chuck is no longer a valid choice, so we just want to have the following:
Bob
Dan
Karen

However, this doesn't change the fact that we have history data that has Chuck in it.  Currently, if the user opens a record that is referring to Chuck, we try to assign the value 'Chuck' to the drop down list, but it will crash since it's not in the available options.

Basically, what I want is to have the combo populated with the valid choices, and if I programatically attempt to set the selected value to a value that doesn't exist in the combo, I don't want to crash, I want to automatically add that value to the list so that we can support historical records.

Does this make sense?  Is it possible with the Combo?
0
Rosi
Telerik team
answered on 16 Oct 2008, 01:08 PM
Hi James,

You can add items programmatically and then bind RadCombobox to your datasource.
To do this you should enable the AppendDataBoundItems property (False by default) of RadComboBox.

How you can add items programmatically please see here.

Sincerely,
Rosi
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
James
Top achievements
Rank 1
answered on 17 Oct 2008, 06:20 AM
Hi Rosi

That's one way to work around it.  However, what I want is:

I have a combobox (cmbNames) with:
Bob
Chuck
Dan
Karen


I then call this code:
cmbNames.SelectedValue = "Johnson";

And I get "Selection out of range Parameter name: value".   I understand that this is because Johnson isn't in the combobox entries, however I'd like for the combobox to automatically add an entry in the combobox for Johnson, rather than crashing.

Is this possible?



0
Accepted
Rosi
Telerik team
answered on 20 Oct 2008, 07:12 AM
Hi James,

This functionality is not supported by RadComboBox.

However you can use try-catch block to catch the exception and add programmatically an item with this value:

For example:
try
{
    cmbNames.SelectedValue = "Johnson";
}
catch (Exception e)
{

    cmbNames.Items.Add( new RadComboBoxItem("Johnson","Johnson"));
    cmbNames.SelectedValue = "Johnson";
}

OR you can check does this value exist before selecting it:

if( cmbNames.FindItemByValue("Johnson")!=null )
{
  cmbNames.SelectedValue = "Johnson";
}

Regards,
Rosi
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Kenyon Fox
Top achievements
Rank 1
answered on 06 Dec 2011, 08:50 PM
I am having this same challenge except my RadComboBox object is contained within a FormView EditItemTemplate. I want to implement the try ... catch handler as described above when the EditItemTemplate binds the SeletedValue of the RadComboBox. I'm not sure how to isolate the SelectedValue bind so as to place my try...catch error handler. Here is how I have my RadComboBox declared within the FormView EditItemTemplate...

<

 

telerik:RadComboBox ID="radcomboSalesperson" runat="server" Width="185px" TabIndex="13" DataSourceID="dsSalespersonList" DataValueField="EmployeeID" DataTextField="EmployeeName" AppendDataBoundItems="true" SelectedValue='<%# Bind("SalesEmployeeID") %>'></telerik:RadComboBox>

 

0
Dimitar Terziev
Telerik team
answered on 09 Dec 2011, 10:30 AM
Hi Kenyon,

In scenarios like your the selected value should be set from code behind, so you could wrap it with a try-catch block as my colleague had suggested. Please try to subscribe to the ModeChanging event of the formview and set the selected value in the event handler function.

Kind regards,
Dimitar Terziev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Jake
Top achievements
Rank 1
answered on 06 Jul 2012, 10:30 AM
I need to be able to do the same thing. The user should be able to free type any entry in the combo box or select from a predefined range of values. This was simple enough in the old access datasheet front end that the datasource was originally linked to..

Does anyone have a completed code sample to do this.? I want to add the value dynamically to the list to prevent selection out of range error..It would be nice if the RadComboBox supported this as I would have thought this is quite a comment event..

0
Darren
Top achievements
Rank 1
answered on 11 Sep 2012, 03:26 PM
Why is the control throwing an exception here?  There should be a way to override that without writing code behind.
0
Kalina
Telerik team
answered on 17 Sep 2012, 11:19 AM
Hello Darren,

Could you please explain in more details your implementation and paste some working code here?
How exactly do you bind the RadComboBox?
What is the exact error that you receive?

Regards,
Kalina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Piotr
Top achievements
Rank 1
answered on 28 Jan 2015, 07:13 AM
maybe this someone will help:

protected void PreventErrorOnbinding(object sender, EventArgs e)
        {
            RadComboBox cb = sender as RadComboBox;
            cb.DataBinding -= new EventHandler(PreventErrorOnbinding);
            cb.AppendDataBoundItems = true;
 
            try
            {
                cb.DataBind();
                cb.Items.Clear();
            }
            catch (ArgumentOutOfRangeException)
            {
                cb.Items.Clear();
                cb.ClearSelection();
                RadComboBoxItem cbI = new RadComboBoxItem("", "0");
                cbI.Selected = true;
                cb.Items.Add(cbI);
            }
        }
and aspx
<telerik:RadComboBox ID="Combo1" runat="server" DataSourceID="SDS" DataTextField="text"   DataValueField="id"  EnableLoadOnDemand="False" Filter="Contains"  SelectedValue='<%# Bind("value") %>' ShowMoreResultsBox="True" 
OnDataBinding="PreventErrorOnbinding">
</telerik:RadComboBox>
Cres
Top achievements
Rank 1
commented on 09 May 2024, 07:40 AM

had this as a great find for the day, thanks a lot! hope it is not too old of a topic/answer to complement :D 

Michael
Top achievements
Rank 1
commented on 24 Jul 2024, 07:31 PM | edited

Still works.  THANKS! for this!
I just wanted to show the Drop down with no selection, when the item is missing!  This is perfect.

I did have to make one change, that is to comment out: //cb.Items.Clear(); in the Try.  With this in, the good matches dont bind.

0
Andrew
Top achievements
Rank 1
answered on 16 Jun 2016, 01:02 PM
I found myself today stuck on this, and I know this thread is dead but I found a workaround that benefited me with this issue.
This would fix all of the above problems:
I've got this in the ItemCreated event of my RadDataForm, but you can use it on the ItemDataBound event of other Rad controls.

protected void ReportDataForm_ItemCreated(object sender, RadDataFormItemEventArgs e)
{
    if(e.Item.IsInEditMode)
    {
        RadDataFormDataItem item = (RadDataFormDataItem)e.Item;
 
        RadComboBox rcb1 = (RadComboBox)item.FindControl("WriterListingComboBox");
 
         //This next section calls a LINQ class that I have set up. The variable is set to an IQueryable.
 
        var curReport = CurrentReport();
        foreach (var CurReport in curReport)
        {
            if (rcb1 != null)
            {
                if (CurReport.Staff_Name != null)
                {
                    if (rcb1.Items.Contains(rcb1.Items.FindItemByValue(CurReport.Staff_Name)))
                    {
                        rcb1.SelectedValue = CurReport.Staff_Name.ToString();
                    }
                    else
                    {
                        rcb1.Items.Insert(0, new RadComboBoxItem(CurReport.Staff_Name.ToString(), CurReport.Staff_Name.ToString()));
                    }
                }
            }
        }
    }
}


0
Serge Aranda
Top achievements
Rank 1
answered on 25 May 2017, 06:05 PM
Excellent, Its working Ok with this proposal. 
Tags
ComboBox
Asked by
James
Top achievements
Rank 1
Answers by
Rosi
Telerik team
James
Top achievements
Rank 1
Kenyon Fox
Top achievements
Rank 1
Dimitar Terziev
Telerik team
Jake
Top achievements
Rank 1
Darren
Top achievements
Rank 1
Kalina
Telerik team
Piotr
Top achievements
Rank 1
Andrew
Top achievements
Rank 1
Serge Aranda
Top achievements
Rank 1
Share this question
or