R – Dropdown box Index issue

asp.netdrop-down-menu

I have one dropdownbox(ddlCountry) containing allcountries,If i select USA it will display grid
displaying Tax information related to USA.If i edit information
in grid and if we change country USA to UK in dropdown box
in the ddlCountry(not the dropdownbox coming in the edit window of grid,,no problem for that) it displaying error like

Specified argument was out of the range of valid values.
Parameter name: ItemHierarchicalIndex
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: ItemHierarchicalIndex

Source Error:

Line 86: }
Line 87:
Line 88: if( rgStateTax.EditItems.Count > 0 )
Line 89: {
Line 90: foreach( GridDataItem item in rgStateTax.Items )

Source File: c:\Projects\ACS\sample.Acs.Administration\UserControls\TaxManager.ascx.cs Line: 88

Stack Trace:

[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: ItemHierarchicalIndex]
Telerik.WebControls.GridItemCollection.get_Item(String hierarchicalIndex) +323
Telerik.WebControls.GridDataItemCollection.get_Item(String hierarchicalIndex) +37
Telerik.WebControls.RadGrid.get_EditItems() +215
sample.Acs.Administration.TaxManager.rgStateTax_PreRender(Object sender, EventArgs e) in c:\Projects\ACS\sample.Acs.Administration\UserControls\TaxManager.ascx.cs:88
System.Web.UI.Control.OnPreRender(EventArgs e) +8682870
System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +31
Telerik.RadGridUtils.RadControl.OnPreRender(EventArgs e) +36
Telerik.RadGridUtils.RadAJAXControl.OnPreRender(EventArgs e) +37
Telerik.WebControls.RadGrid.OnPreRender(EventArgs e) +40
System.Web.UI.Control.PreRenderRecursiveInternal() +80
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

this is the grid(rgstatetax) prerender event

protected void rgStateTax_PreRender( object sender, EventArgs e )
    {
        if( rgStateTax.MasterTableView.IsItemInserted )
        {
            foreach( GridItem item in rgStateTax.Items )
            {
                item.Visible = false;
            }
        }

        if( rgStateTax.EditItems.Count > 0 )
        {
            foreach( GridDataItem item in rgStateTax.Items )
            {
                if( item != rgStateTax.EditItems[0] )
                {
                    item.Visible = false;
                }
            }
        }

Code in UI

protected void ddlCountryTax_SelectedIndexChanged( object sender, EventArgs e )
{
long locationId = ddlCountryTax.SelectedItem.Value.AsLong();

        ContentAdministrationServiceClient client = null;
        List<DCTaxRate> taxRate = null;
        try
        {
            client = new ContentAdministrationServiceClient();
            taxRate = client.GetTaxRatesByCountryIdAndLocationTypeName( locationId, "State" );
            client.Close();
        }
        catch( FaultException )
        {
            AbortClient(client);
            throw;
        }

        rgStateTax.DataSource = taxRate;
        rgStateTax.Rebind();

    }

Code in wrapper layer

public List GetTaxRatesByCountryIdAndLocationTypeName( long countryId, string locationTypeName )
{
DCTaxRateCollection taxRates = new DCTaxRateCollection();
taxRates.GetByCountryIdAndLoactionTypeName( countryId, locationTypeName );

        return taxRates.ToList();
    }

    public void GetByCountryIdAndLoactionTypeName( long countryId, string locationTypeName )
    {
        IBOTaxRateCollection iboTaxRates = new BOTaxRateCollection();
        iboTaxRates.GetByCountryIdAndLocationTypeName( countryId, locationTypeName );

        SetItems( iboTaxRates );
    }

In Bo layer

    public void GetByCountryIdAndLocationTypeName( long countryId, string locationTypeName )
    {
        ISingleResult<TaxRate> taxRates = Database.TaxRateReadByCountryIdAndLocationTypeName( countryId, locationTypeName );
        PopulateCollection( taxRates );
    }

Best Answer

This occurs when the list of values in the dropdown does not include the value being bound to the SelectedValue property. If this is because you have a null value you could get the data adapter to return an empty string when the field is null (if that is the problem) and then insert a value in the dropdown list to match:

<asp:DropDownList ID="dd_bound" runat="server" AppendDataBoundItems="True">
    <asp:ListItem Value="" Text="Select one..." />
</asp:DropDownList>

Note the AppendDataBoundItems value.