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

Group udate database

1 Answer 7 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Allan
Top achievements
Rank 2
Allan asked on 10 Mar 2013, 10:41 PM
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")
 
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()
        Next
 
    Next
 
End Sub

As always any help much appreciated.


1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 14 Mar 2013, 06:58 AM
Hi,

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.

Regards,
Andrey
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.
Tags
Grid
Asked by
Allan
Top achievements
Rank 2
Answers by
Andrey
Telerik team
Share this question
or