We help IT Professionals succeed at work.

Index was out of range. Must be non-negative and less than the size of the collection. Parameter: Index

prasadpbr
prasadpbr asked
on
Dear Telerik Team,

When I am trying to display data using radgrid I am getting error like "Index was out of range. Must be non-negative and less than the size of the collection. Parameter: Index".

Here is my code

telerik:RadGrid ID="gvCktMap" BorderColor="White" runat="server" AutoGenerateColumns="true" 
                                AllowSorting="true" BackColor="White" AllowPaging="true" PageSize="25" GridLines="None" 
                                OnPageIndexChanging="gvCktMap_PageIndexChanging" OnRowCancelingEdit="gvCktMap_RowCancelingEdit" 
                                OnRowCommand="gvCktMap_RowCommand" OnRowUpdating="gvCktMap_RowUpdating" OnRowDataBound="gvCktMap_RowDataBound" 
                                OnSorting="gvCktMap_Sorting" OnRowEditing="gvCktMap_RowEditing" ShowGroupPanel="True" 
                                EnableHeaderContextMenu="true" EnableHeaderContextFilterMenu="true" AllowMultiRowSelection="true" 
                                AllowFilteringByColumn="True" AllowCustomPaging="false" OnItemCreated="gvCktMap_ItemCreated" 
                                EnableViewState="false" OnNeedDataSource="gvCktMap_NeedDataSource" OnItemUpdated="gvCktMap_ItemUpdated" 
                                > 
                                <MasterTableView DataKeyNames="sId" UseAllDataFields="true"> 
                                    <Columns> 
                                        <telerik:GridBoundColumn UniqueName="sId" HeaderText="sId" DataField="sId" Visible="false"> 
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="orderId" HeaderText="orderId" DataField="orderId"> 
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="REJ" HeaderText="REJ" DataField="REJ"> 
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="Desc" HeaderText="Desc" DataField="Desc"> 
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="CustomerName" HeaderText="CustomerName" DataField="CustomerName"> 
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="MarketName" HeaderText="MarketName" DataField="MarketName"> 
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="HeadendName" HeaderText="HeadendName" DataField="HeadendName"> 
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="SiteName" HeaderText="SiteName" DataField="SiteName"> 
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="TaskStatus" HeaderText="TaskStatus" DataField="TaskStatus"> 
                                        </telerik:GridBoundColumn> 
                                    </Columns> 
                            </telerik:RadGrid> 

Open in new window



Here is my code behind
private void bingGrid() 
    { 
        try 
        { 
            gvCktMap.Columns.Clear(); 
            DataSet dsResult = new DataSet(); 
            DataSet dsEditItems = new DataSet(); 
            dsEditItems.ReadXml(Server.MapPath("XMLS/" + Session["TaskID"].ToString() + ".xml")); 
 
            clsSearch_BL clsObj = new clsSearch_BL(); 
            clsObj.TaskID = (string)Session["TaskID"]; 
            clsObj.CustName = (string)Session["CustName"]; 
            clsObj.MarketName = (string)Session["MarketName"]; 
            clsObj.HeadendName = (string)Session["HeadendName"]; 
            clsObj.SiteName = (string)Session["SiteName"]; 
            clsObj.TaskStatus = (string)Session["TaskStatus"]; 
            clsObj.OrdType = (string)Session["OrdType"]; 
            clsObj.OrdStatus = (string)Session["OrdStatus"]; 
            clsObj.ProName = (string)Session["ProName"]; 
            clsObj.LOC = (string)Session["LOC"]; 
 
            dsResult = clsObj.getSearchResults_BL(clsObj); 
            Session["SearchRes"] = dsResult; 
            DataTable dtFilter = new DataTable(); 
            DataColumn dtCol = new DataColumn("FilterBy"); 
            dtFilter.Columns.Add(dtCol); 
            dtCol = new DataColumn("DataType"); 
            dtFilter.Columns.Add(dtCol); 
            gvCktMap.DataSource = dsResult; 
            gvCktMap.DataBind(); 
    } 
        catch (Exception ex) 
        { 
        } 
    } 

Open in new window



If i remove <MasterTableView></MasterTableView> Its working fine without any error. But for some reasons i need to use <MasterTableView></MasterTableView> Can anyone help me out to fix this error.

Thanks In Advance

Comment
Watch Question

Commented:
Can you post your stacktrace as well?

Author

Commented:
Hi Shobhitq,

How to get stacktrace? Can you please tell me. I dont know how to do it?
Senior Software Engineer
Commented:

1) In VS View -> Output Window (may be it is already open, but for safe side)
2) rewrite you code like this:

replace:

       catch (Exception ex)
        {

        }
with:

            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }


and then see output window. you will see many lines but the last few lines will be referring the error causing.

Author

Commented:
This is my stachtrace


Stack Trace:


[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.Collections.ArrayList.get_Item(Int32 index) +7483656
   Telerik.Web.UI.GridColumnCollection.System.Web.UI.IStateManager.SaveViewState() +244
   Telerik.Web.UI.GridTableView.GetStructureState() +263
   Telerik.Web.UI.RadGrid.SaveTableViewStructure(ArrayList stateList, GridTableView tableView) +81
   Telerik.Web.UI.RadGrid.SaveViewState() +128
   System.Web.UI.Control.SaveViewStateRecursive() +187
   System.Web.UI.Control.SaveViewStateRecursive() +106
   System.Web.UI.Control.SaveViewStateRecursive() +106
   System.Web.UI.Control.SaveViewStateRecursive() +106
   System.Web.UI.Control.SaveViewStateRecursive() +106
   System.Web.UI.Control.SaveViewStateRecursive() +106
   System.Web.UI.Control.SaveViewStateRecursive() +106
   System.Web.UI.Page.SaveAllState() +168
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1099

 
Shahan AyyubSenior Software Engineer

Commented:
Could you please point out the line from your code which is raising this exception.

Author

Commented:
HI Shahan,

Thanks for your help. I got solution. Thanks for your help.