Group udate database

2 posts, 0 answers
  1. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 10 Mar 2013 Link to this post

    I have a Grid that is grouped. When I update the database based on the group I get the foolwoing error:

    "Unable to cast object of type 'Telerik.Web.UI.GridGroupFooterItem' to type 'Telerik.Web.UI.GridDataItem'. "

    The database is updated even though the error is thrown.

    Here is my code.
    Private Sub btn_ApproveAll_Click(sender As Object, e As EventArgs)
        Dim item As GridGroupHeaderItem = DirectCast(rgd_ProjectPlanTasksApprove.MasterTableView.GetItems(GridItemType.GroupHeader)(0), GridGroupHeaderItem)
        For Each groupHeader As GridGroupHeaderItem In rgd_ProjectPlanTasksApprove.MasterTableView.GetItems(GridItemType.GroupHeader)
            Dim children As GridItem() = groupHeader.GetChildItems()
            For Each child As GridDataItem In children
                Dim IDTaskUpdates As String = child("IDTaskUpdates").Text
                Dim sql As String
                Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("SharePoint_ConnectionString").ConnectionString()
                sql = "UPDATE Task_Updates SET TaskUpdateApproved = 1, ApprovedDate = @ApprovedDate, ModifiedDate = @ModifiedDate  WHERE IDTaskUpdates = @IDTaskUpdates"
                Dim cn As New SqlConnection(strConnString)
                Dim cmd As New SqlCommand(sql, cn)
                cmd.Parameters.Add("@IDTaskUpdates", SqlDbType.Int).Value = IDTaskUpdates
                cmd.Parameters.Add("@ApprovedDate", SqlDbType.DateTime).Value = Date.Now
                cmd.Parameters.Add("@ModifiedDate", SqlDbType.DateTime).Value = Date.Now
                cmd.Parameters.Add("@IDModifiedUser", SqlDbType.NVarChar, 36).Value = Session("ActiveDirID")
    End Sub

    As always any help much appreciated.

  2. Andrey
    Andrey avatar
    836 posts

    Posted 14 Mar 2013 Link to this post


    Most probably the problem comes from this line of code:

    For Each child As GridDataItem In children

    GetChildItems returns an array of GridItem objects. However, this is the base class for all items and thus the items in this array could be of any kind. One of these items is of type of GridGroupHeaderItem and that is why it could not be casted to GridDataItem type.

    In order to resolve this issue you need to loop over the collection with simple For loop and check whether the currently iterated item could be casted to GridDataItem, if the cast is successful you continue with your logic otherwise you call continue.

    the Telerik team
    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 their blog feed now.
Back to Top