Inserting Multiple Related Entities from a single formview RRS feed

  • Question

  • I have a Data Source

              <asp:EntityDataSource ID="ApplicationDetails" runat="server" ConnectionString="name=Entities" DefaultContainerName="Entities"
                EntitySetName="ResellerApplications" Include="Reseller,Status,HowHeardAbout,Reseller.CustomerAddress,Reseller.CustomerAddress.Country"
                AutoGenerateWhereClause="True" OnContextCreating="DS_ContextCreating" OnContextDisposing="DS_ContextDisposing">
                    <asp:ControlParameter ControlID="ApplicationsGrid" PropertyName="SelectedValue" Name="Id" Type="Int32" />


    a form view is linked to this datasource, which binds like this in a textbox (in both item and insert item templates)

    Text='<%# Bind("Reseller.Company") %>'

    On insert I get an exception that the Reseller.Company property does not exist (it displays properly when simply viewing)


    The ResellerApplication requires a Reseller which requires and address.


    The problem is that I would like the Reseller and address entities to be created when inserting a new application.


    Is this possible, or will I need to break down the formview into 3 related forms with seperate datasources and

    insert in sequence and address, then a reseller, then a reseller application?

    Is there a different solution to simultaneous creation of related entities?



    Tuesday, September 6, 2011 10:52 PM

All replies

  • Hi,

    I think you sould use EntityDataSource.Inserting event and add newly created related entities to the inserting entity via the event arguments object, something like this:


        1 void Source_Inserting(object sender, EntityDataSourceChangingEventArgs e)

        2 {

        3     ((BusinessEntity)e.Entity).Company = new HRCompany

        4                                              {

        5                                                  NameEng = "New Company"

        6                                              };

        7 }


    you can use the binding expressions when the property is of a primitive type or like this 


    <asp:DropDownList runat="server" ID="ListJobProfiles" DataSourceID="SourceJobProfiles" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("Profile.Id") %>' />




    Clarity VS Precision
    Wednesday, September 7, 2011 8:10 PM
  • Thanks for your reply

    We are changing strategies to not use EntityDataSource, so I am not working on it anymore. 

    However, that solution didn’t work because the data source inserting event did not happen before the error that the property was not valid.

    Thursday, September 8, 2011 3:19 PM
  • Hello,

    Any update? Could you please share your update workaround here? In addition, in your previous reply, I made a test on my side, it looks OK. If you need further support for previous question, could you please provide demo application to us to analyze your question more detail? My mail: v-xugong@microsoft.com


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 12, 2011 3:12 AM
  • Hi,

    this kind of error occurs when you specify a wrong value for a property, e.g. string instead of integer, or null for not nullable field etc.

    So you might want to check that as well.


    Thank you

    Clarity VS Precision
    Monday, September 12, 2011 6:22 AM