Fixing EntityDataSourceWrapper Error in EF (VB Version)

I lost a lot of time today on this error:

‘EntityDataSourceWrapper' does not contain a property with the name …'

The project is ASP.NET 3.5 using Entity Framework and VB. I’m using the Telerik RadControls for ASP.NET AJAX (a sweet suite!). In my app, clicking a row’s Edit button on the RADGrid  opens the popup Edit form for updating the data.

It seemed like a simple task to get the DataItem for the row (item), extract a value, and use that value as the SelectedValue of radiobuttonlist.  It turns out that you need heroic measures to get a value out of e.Item.DataItem in Entity Framework! Sheesh!

Here’s the VB version of LinqHelpers as posted by Diego B Vega. It noses around in the DataItem to return a TEntity (whatever that is!). I’m still trying to figure this code out, but at least I’m productive again!

' LinqHelpers.vb - Put it into the App_Code folder
Namespace LinqHelpers
    Public NotInheritable Class EntityDataSourceExtensions
        Public Shared Function GetItemObject _
        (Of TEntity As Class)(ByVal dataItem As Object) As TEntity
            Dim entity As TEntity = TryCast(dataItem, TEntity)
            If entity IsNot Nothing Then
                Return entity
            End If
            Dim td = TryCast(dataItem, ICustomTypeDescriptor)
            If td IsNot Nothing Then
                Return DirectCast(td.GetPropertyOwner(Nothing), TEntity)
            End If
            Return Nothing
        End Function
    End Class
End Namespace

Once you’ve got a way to mess around inside the DataItem, you can fix up the radiobuttonlist (a dropdownlist would be similar). Do this in the RadGrid’s ItemCreated event as shown:

Protected Sub RadGrid1_ItemCreated _
  (ByVal sender As Object, ByVal e As  _
   Telerik.Web.UI.GridItemEventArgs) _
   Handles RadGrid1.ItemCreated
    ' Sets the selected values for radiobuttons and
    ' ddls when displaying the edit form
    If (TypeOf e.Item Is GridEditFormItem) And _
        e.Item.IsInEditMode And _
        (Not IsNothing(e.Item.DataItem)) Then
        ' Get a reference to the editformitem
        Dim editFormItem As GridEditFormItem = _
        CType(e.Item, GridEditFormItem)
        ' Get the entity that's being used
        'with this screen (requires a LINQ helper)
        Dim entity = LinqHelpers.EntityDataSourceExtensions. _
        GetItemObject(Of CMS_Item)(e.Item.DataItem)

        ' Get a reference to the radiobutton list
        ' and set its value
        Dim rbl As RadioButtonList = _
        rbl.SelectedValue = entity.ItemType

        ' Get a reference to the Product radiobutton
        ' and set its value
        rbl = editFormItem.FindControl("radProduct")
        rbl.SelectedValue = entity.Product.ToString()

        ' Set the Stocked radiobuttonlist
        rbl = editFormItem.FindControl("radStocked")
        rbl.SelectedValue = entity.Stocked.ToString()

     End If
End Sub

Thanks to all the community contributions that help solve these issues. Here’s a thread with an answer by Microsoft’s Diego B Vega that got me over the hump:

Am I the only one who spins on these things???



No Comments