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

Building multiple radgrids - index out of range

3 Answers 123 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steven Webster
Top achievements
Rank 1
Steven Webster asked on 28 Sep 2010, 11:02 AM
Hi there

I have a page with a radcombobox and a placeholder. When a new value is selected from the radcombobox a stored procedure runs which returns multiple result sets based on the selected value. For each one of the result sets which contains at least one row, I need to build a dynamic radgrid. The first set of radgrids always display fine, but sometimes when I select a new value I get an "index was out of range error". I've read some articles that say I need to create dynamic radgrids in OnInit, but I can't get the radcombobox selectedindex in there (at least I can't seem to - maybe someone might have an idea on this). Hopefully someone will have come across this issue and be able to assist me.

Thanks

Here's my aspx code:

 

 

<telerik:RadComboBox ID="LabsDDL" runat="server" AutoPostBack="True" DataSourceID="dsLabs"
    DataTextField="Name" DataValueField="DataSourceID" Width="250px" Height="150px"
    Skin="Outlook">
</telerik:RadComboBox>
</br></br>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>


Here's my vb code:

Protected Sub LabsDDL_SelectedIndexChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) Handles LabsDDL.SelectedIndexChanged
    BuildGrids(LabsDDL.SelectedItem.Value)
End Sub
Protected Overrides Sub OnInit(ByVal e As EventArgs)
    PlaceHolder1.Controls.Clear()
End Sub
Protected Sub BuildGrids(ByVal LabID As Integer)
    Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("StagingConnectionString").ConnectionString)
    Dim cmd As New SqlCommand()
    cmd.Connection = conn
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "spLookupChecksNew"
    'Create and add a parameter to Parameters collection for the stored procedure.
    cmd.Parameters.Add(New SqlParameter("@Lab", SqlDbType.Int))
    'Assign the search value to the parameter.
    cmd.Parameters("@Lab").Value = LabID
    Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    ds.Clear()
    ds.Tables.Clear()
    da.Fill(ds)
    Dim i As Integer
    i = 0
    'This will fill dataset with three tables.
    For Each DataTable In ds.Tables
        If ds.Tables(i).Rows.Count > 0 Then
            Dim myConn As New SqlConnection(ConfigurationManager.ConnectionStrings("StagingConnectionString").ConnectionString)
            myConn.Open()
            Dim CheckID As Integer
            Dim CheckName As String
            Dim CheckType As Integer
            Dim ColumnName As String
            Dim AssociatedTable As String
            Dim ParmDefinition As String
            CheckID = CInt(ds.Tables(i).Rows(0)(0))
            Dim cmdOptions As New SqlCommand("spGetCheckOptions", myConn)
            cmdOptions.CommandType = CommandType.StoredProcedure
            cmdOptions.Parameters.AddWithValue("@CheckID", CheckID.ToString)
            Dim reader As SqlDataReader = cmdOptions.ExecuteReader()
            While reader.Read
                If (IsDBNull(reader(0))) Then
                    CheckName = ""
                Else
                    CheckName = reader(0).ToString()
                End If
                If (IsDBNull(reader(1))) Then
                    CheckType = 0
                Else
                    CheckType = reader(1)
                End If
                If (IsDBNull(reader(2))) Then
                    ColumnName = ""
                Else
                    ColumnName = reader(2).ToString()
                End If
                If (IsDBNull(reader(3))) Then
                    AssociatedTable = ""
                Else
                    AssociatedTable = reader(3).ToString()
                End If
                If (IsDBNull(reader(5))) Then
                    ParmDefinition = ""
                Else
                    ParmDefinition = reader(5).ToString()
                End If
            End While
            reader.Close()
            cmdOptions.Dispose()
            myConn.Close()
            myConn.Dispose()
            Dim myGridView As New RadGrid
            myGridView = New RadGrid()
            Dim lblMessage As New Literal
            Try
                Dim ltrTableStart As New Literal
                ltrTableStart.Text = "<table><tr><td>"
                Dim ltrTableEnd As New Literal
                ltrTableEnd.Text = "</tr></td></table>"
                Dim literalheader As New Literal
                literalheader.Text = "<div class='AutoShrink'><h5>" & CheckName & "</h5>"
                Dim literalfooter As New Literal
                literalfooter.Text = "</div>"
                myGridView.EnableViewState = False
                myGridView.DataSource = ds.Tables(i)
                Select Case CheckType
                    Case 1
                        Dim templateColumnName As String = ColumnName
                        Dim templateColumn As New GridTemplateColumn()
                        templateColumn.ItemTemplate = New MyTemplate(templateColumnName, LabsDDL.SelectedValue)
                        templateColumn.HeaderText = "Map value" 'templateColumnName
                        templateColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center
                        templateColumn.ItemStyle.VerticalAlign = VerticalAlign.Middle
                        myGridView.Columns.Add(templateColumn)
                End Select
                Dim viewColumnName As String = ColumnName
                Dim viewColumn As New GridTemplateColumn()
                viewColumn.ItemTemplate = New ViewTemplate(viewColumnName, LabsDDL.SelectedValue, ParmDefinition)
                viewColumn.HeaderText = "View Records" 'viewColumnName
                viewColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center
                viewColumn.ItemStyle.VerticalAlign = VerticalAlign.Middle
                myGridView.Columns.Add(viewColumn)
                myGridView.ID = "rgErrors" & i.ToString
                myGridView.Skin = "Outlook"
                myGridView.CssClass = "AutoShrink"
                myGridView.MasterTableView.TableLayout = GridTableLayout.Auto
                myGridView.MasterTableView.CssClass = "AutoShrink"
                myGridView.ClientSettings.Resizing.ClipCellContentOnResize = True
                myGridView.DataBind()
                PlaceHolder1.Controls.Add(ltrTableStart)
                PlaceHolder1.Controls.Add(literalheader)
                PlaceHolder1.Controls.Add(myGridView)
                PlaceHolder1.Controls.Add(literalfooter)
                PlaceHolder1.Controls.Add(ltrTableEnd)
                myConn.Close()
                myConn.Dispose()
                myGridView.MasterTableView.Dispose()
                myGridView.Dispose()
            Catch ex As Exception
                lblMessage.Text = "<h5 style=""color: red"">Error building grid: " & CheckName & " (MESSAGE:" & ex.Message.ToString & ")</h5>"
                PlaceHolder1.Controls.Add(lblMessage)
            End Try
        End If
        i = i + 1
    Next
    cmd.Dispose()
    conn.Close()
    conn.Dispose()
End Sub

This is the 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) +7485640
   Telerik.Web.UI.GridColumnCollection.System.Web.UI.IStateManager.SaveViewState() +215
   Telerik.Web.UI.GridTableView.GetStructureState() +257
   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.Control.SaveViewStateRecursive() +106
   System.Web.UI.Page.SaveAllState() +168
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1099

3 Answers, 1 is accepted

Sort by
0
Accepted
Pavlina
Telerik team
answered on 28 Sep 2010, 11:23 AM
Hello Steven,

In order to make sure that the exception does not happen, you need to create the structure of the control properly:
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

If you would like to alter the structure dynamically, you can use the following approach:
http://www.telerik.com/help/aspnet-ajax/grdchanginggridstructuredynamically.html

Alternatively, you can declare the whole structure statically, in the aspx page.
Also, in order to make sure that the operations in the control are performed as expected, you need to take advantage of the NeedDataSource event handler, or a datasource control, to populate the grid with data.

I hope this information gets you started properly.

Sincerely yours,
Pavlina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Steven Webster
Top achievements
Rank 1
answered on 28 Sep 2010, 11:46 AM
Thanks a million for the quick response Pavlina. I needed to set EnableColumnsViewState to False to fix the issue (taken from this post: http://www.telerik.com/help/aspnet-ajax/grdchanginggridstructuredynamically.html
0
Pavlina
Telerik team
answered on 28 Sep 2010, 12:23 PM
Hello Steven,

I am glad I could help, in case you experience any further problems or you have additional questions, do not hesitate to contact us again.

All the best,
Pavlina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Steven Webster
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Steven Webster
Top achievements
Rank 1
Share this question
or