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

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

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
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> 



Here is my code behind
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
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) 
        { 
        } 
    } 



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

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


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.
Random Solutions  
 
programming4us programming4us