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

13 posts, 1 answers
  1. James
    James avatar
    41 posts
    Member since:
    Jul 2007

    Posted 15 Oct 2008 Link to this post

    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?
  2. Rosi
    Admin
    Rosi avatar
    2527 posts

    Posted 16 Oct 2008 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. James
    James avatar
    41 posts
    Member since:
    Jul 2007

    Posted 16 Oct 2008 Link to this post

    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?
  5. Rosi
    Admin
    Rosi avatar
    2527 posts

    Posted 16 Oct 2008 Link to this post

    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.
  6. James
    James avatar
    41 posts
    Member since:
    Jul 2007

    Posted 17 Oct 2008 Link to this post

    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?



  7. Answer
    Rosi
    Admin
    Rosi avatar
    2527 posts

    Posted 20 Oct 2008 Link to this post

    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.
  8. Kenyon Fox
    Kenyon Fox avatar
    9 posts
    Member since:
    Nov 2005

    Posted 06 Dec 2011 Link to this post

    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>

     

  9. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 09 Dec 2011 Link to this post

    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
  10. Jake
    Jake avatar
    15 posts
    Member since:
    Aug 2012

    Posted 06 Jul 2012 Link to this post

    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..

  11. Darren
    Darren avatar
    20 posts
    Member since:
    Sep 2011

    Posted 11 Sep 2012 Link to this post

    Why is the control throwing an exception here?  There should be a way to override that without writing code behind.
  12. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 17 Sep 2012 Link to this post

    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.
  13. Piotr
    Piotr avatar
    5 posts
    Member since:
    Aug 2012

    Posted 28 Jan 2015 in reply to James Link to this post

    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>
  14. Andrew
    Andrew avatar
    1 posts
    Member since:
    May 2014

    Posted 16 Jun in reply to Piotr Link to this post

    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()));
                        }
                    }
                }
            }
        }
    }


Back to Top
UI for ASP.NET Ajax is Ready for VS 2017