none
DataBind Entity Framework with EntityDataSource RRS feed

  • Question

  • Hi,

    This is probably an easy problem, but I can't seem to find the information I need!

    I have a table / object which I have created with the EF, the columns are:

    ID (pk), CarRegistration(fk), EmployeeID, DateFrom, DateTo, CommentID, Allowance

    I wanted the comment from the comment table using the CommentID, so from the video on asp.net, I just evaluated it

    Eval("Comment.Comment1")

    Which worked fine,

    But for the EditItemTemplate where I try to use Bind() it doesn't work.

    I looked in the ItemUpdating Event, and I noticed it was passing the ID, and the columns but added a column "Comment.Comment1" which then died.

    1[NullReferenceException: Object reference not set to an instance of an object.] 
    2   System.Web.UI.WebControls.EntityDataSourceView.ConvertWCProperty(IDictionary values, Dictionary`2 convertedValues, List`1 visitedProperties, PropertyDescriptor pd, ParameterCollection referenceParameters, Dictionary`2& exceptions) +35 
    3   System.Web.UI.WebControls.EntityDataSourceView.ConvertProperties(IDictionary values, PropertyDescriptorCollection propertyDescriptors, ParameterCollection referenceParameters) +216 
    4   System.Web.UI.WebControls.EntityDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +237 
    5   System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +92 
    6   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +907 
    7   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +704 
    8   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95 
    9   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 
    10   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +123 
    11   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 
    12   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118 
    13   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135 
    14   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
    15   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
    16   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175 
    17   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 
    18 

    I have looked for information, but most examples are using the ID on another DataSource and update that way. But I'm sure there must be a way of editing the text inline?

    The only workaround I did, was roll my own method inside the ItemUpdating and use the EF to update the row, but this will not scale well.


    Cheers, Sarkie

    Sarkie
    Wednesday, March 18, 2009 12:51 PM

Answers

  • Hello Sarkie,

    Sorry to see your question went unanswered for a long time. The EntityDataSource actually uses a special “flattening” mechanism to expose key properties of referenced entities to ASP.NET databinding, even though those key properties are not really part of the entity. This makes it possible to use Bind() or BoundFields (i.e. GridView columns) to modify the values of them.

    For your example, the Comment.CommentID can be used by Bind() and so its value can be modified (i.e. you could use that to choose a different comment using a DropDownList control), but Comment.Comment1 is not actually a top level property on the source entity, and therefore you cannot pass it to Bind(). It is available for read-only binding trough Eval() however, because Eval() knows how to drill through the Comment navigation property to get the value of the Comment1 property of the related Comment.

    If what you need to do is to edit the comment text itself, you could create a master-detail page with a separate EntityDataSource configured to bind to the Comments EntitySet, and with a control parameter in the WhereParameters linked to the value of the Comment.CommentID on the master grid.

    Hope this helps,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, April 13, 2009 4:44 AM
    Moderator
  • Hello Again!


    Yes it as because of the flattening of the properties, I actually use the OnSelectedIndexChange event and hooked it up to a View and inside the multiview, to get the desired effect I needed!

    But thanks again for the response!

    Sarkie
    Sarkie
    Monday, April 13, 2009 11:55 AM

All replies

  • Hello Sarkie,

    Sorry to see your question went unanswered for a long time. The EntityDataSource actually uses a special “flattening” mechanism to expose key properties of referenced entities to ASP.NET databinding, even though those key properties are not really part of the entity. This makes it possible to use Bind() or BoundFields (i.e. GridView columns) to modify the values of them.

    For your example, the Comment.CommentID can be used by Bind() and so its value can be modified (i.e. you could use that to choose a different comment using a DropDownList control), but Comment.Comment1 is not actually a top level property on the source entity, and therefore you cannot pass it to Bind(). It is available for read-only binding trough Eval() however, because Eval() knows how to drill through the Comment navigation property to get the value of the Comment1 property of the related Comment.

    If what you need to do is to edit the comment text itself, you could create a master-detail page with a separate EntityDataSource configured to bind to the Comments EntitySet, and with a control parameter in the WhereParameters linked to the value of the Comment.CommentID on the master grid.

    Hope this helps,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, April 13, 2009 4:44 AM
    Moderator
  • Hello Again!


    Yes it as because of the flattening of the properties, I actually use the OnSelectedIndexChange event and hooked it up to a View and inside the multiview, to get the desired effect I needed!

    But thanks again for the response!

    Sarkie
    Sarkie
    Monday, April 13, 2009 11:55 AM