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

Index Outside Bounds of Array

2 Answers 272 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jamie
Top achievements
Rank 2
Jamie asked on 01 Nov 2013, 04:56 PM
The rows in my grid need to be updated with the value of a checkbox. The rad dropdown gets a value, then based off of that value, the checkbox gets an assigned value of 'active'(checked) or 'not active'(unchecked). When the user clicks on 'Save', each checked box will then take the ID and associate it to the ID of the dropdown list. Debugging stops at the GridCommandItem line and throws the error `Index was outside the bounds of the array.` Can someone please help me out here? I don't work with arrays much so I don't know how to fix this.

<telerik:RadDropDownList ID="MarketDropDownList" runat="server" DefaultMessage="--Select Market--" Width="200px" DataSourceID="ddlDataSource" DataTextField="MarketName" DataValueField="MarketID" AutoPostBack="true" OnSelectedIndexChanged="MarketDropDownList_SelectedIndexChanged">
</telerik:RadDropDownList>
 
<telerik:RadGrid ID="radProductsGrid" runat="server" AllowPaging="True"
                AllowSorting="True" AutoGenerateColumns="False" GridLines="None"
                OnNeedDataSource="radProductsGrid_NeedDataSource"
                OnSortCommand="radProductsGrid_SortCommand" PageSize="100" CellSpacing="0">
  <MasterTableView AllowMultiColumnSorting="false"
                    AllowNaturalSort="false" AllowSorting="true" AutoGenerateColumns="false"
                    Width="100%">
 
                    <CommandItemTemplate>
                        <telerik:RadComboBox ID="RadComboBox1"  DataTextField="ProductID" DataValueField="ProductID" runat="server"
                        </telerik:RadComboBox>
                    </CommandItemTemplate>
  
                    <Columns>
                        <telerik:GridTemplateColumn AllowFiltering="true" HeaderText="Product in Market"
                            ReadOnly="true" UniqueName="TemplateColumnCheckBox">
                            <HeaderTemplate>
                                <asp:CheckBox ID="selectAllCheckboxes" runat="server"
                                    onClick="javascript:SelectDeselectAllCheckboxes(this);" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="ProductInMarketCheckBox" runat="server" />
                                <asp:HiddenField runat="server" ID="ProductID" Value="ProductID" />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
 
                        <telerik:GridBoundColumn AllowFiltering="true" DataField="ItemNumber"
                            HeaderText="Item Number" ReadOnly="true" SortExpression="ItemNumber"
                            UniqueName="ItemNumber">
                        </telerik:GridBoundColumn>
 
                        <telerik:GridBoundColumn AllowFiltering="true" DataField="ProductName"
                            HeaderText="Product Name" ReadOnly="true" SortExpression="ProductName"
                            UniqueName="ProductName">
                        </telerik:GridBoundColumn>
 
                        <telerik:GridBoundColumn AllowFiltering="true" DataField="CategoryName"
                            HeaderText="Category Name" ReadOnly="true" SortExpression="CategoryName"
                            UniqueName="CategoryName">
                        </telerik:GridBoundColumn>
 
                    </Columns>
  </MasterTableView>
</telerik:RadGrid>
<asp:Button ID="btnAddProductToMarket" runat="server" border="0" Text="Save" ToolTip="Select Checkbox and Add Product to Market" OnClick="btnAddProductToMarket_Click" />


protected void btnAddProductToMarket_Click(object sender, EventArgs e)
        {
            int[] myArray = new int[0];
            try
            {
                GridCommandItem cmdItem = (GridCommandItem)radProductsGrid.MasterTableView.GetItems(GridItemType.CommandItem)[0];
                RadComboBox combo = (RadComboBox)cmdItem.FindControl("RadComboBox1");
                string strtxt = combo.Text;
                foreach (GridDataItem items in radProductsGrid.Items)
                {
                    if (((CheckBox)items["TemplateColumnCheckBox"].FindControl("ProductInMarketCheckBox")).Checked)
                    {
                        HiddenField ProductID = null;
                        ProductID = (HiddenField)items.FindControl("ProductID");
                        Array.Resize(ref myArray, myArray.Length + 1);
                        myArray[myArray.Length - 1] = new int();
                        myArray[myArray.Length - 1] = int.Parse(ProductID.Value);
                        //update query
                        String ConnString = ConfigurationManager.ConnectionStrings["DBConnectingString"].ConnectionString;
                        SqlConnection conn = new SqlConnection(ConnString);
                        String query = @"UPDATE vPanel_MarketMappings SET ProductID = @ProductID, MarketID = @MarketID";
                        using (SqlCommand cmd = new SqlCommand(query,conn))
                        {
                            cmd.Parameters.AddWithValue("@ProductID", ProductID);
                            cmd.Parameters.AddWithValue("@MarketID", (CheckBox)items.FindControl("ProductInMarketCheckBox"));
                            conn.Open();
                            cmd.ExecuteNonQuery();
                            conn.Close();
                        }
                    }
                }
            }
            catch ( Exception ex )
            {
                LogException( Logging.PageType.ProductManage, Logging.MessageType.Exception, ex.ToString() );
                UIUtils.ShowMessageToUser( "OnErrorMesg", this.Page );
            }
        }

2 Answers, 1 is accepted

Sort by
0
Jamie
Top achievements
Rank 2
answered on 01 Nov 2013, 05:25 PM
I commented out the GridCommandItem lines and the 2 other lines underneath it because they don't seem necessary as I look at the code more. The debugger now stops at the line myArray[myArray.Length - 1] = int.Parse(ProductID.Value) saying 'Input string not in the correct format.'

Please help. Thanks in advance.
0
Konstantin Dikov
Telerik team
answered on 06 Nov 2013, 12:46 PM
Hi Jamie,

The reason behind that error is that you have set a "ProductID" value to the hidden field within the ItemTemplate and you are trying to parse that string to an integer.

To have the actual "ProductID" value of the data item you will have to change your hidden field setting with the following:
<asp:HiddenField runat="server" ID="ProductID" Value='<%#Eval("ID") %>' />

Regarding the issue with GridCommandItem. You will have to show the CommandItem by setting the "MasterTableView-CommandItemDisplay" to "Bottom/Top or TopAndBottom" in order to be able to reference the controls in it.

Hope that helps.

 

Regards,
Konstantin Dikov
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Jamie
Top achievements
Rank 2
Answers by
Jamie
Top achievements
Rank 2
Konstantin Dikov
Telerik team
Share this question
or