CodeSmith Community
Your Code. Your Way. Faster!

One-to-One Relationship and InsertItemTemplate

Latest post 10-04-2006 8:16 PM by velum. 8 replies.
  • 09-14-2006 1:01 PM

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    One-to-One Relationship and InsertItemTemplate

    Hi All!

    Is it possible to have a One-to-One Relationship with two nested FormViews in Insert mode? WHat I want to achieve is to insert information for a Client and his Address on a single aspx page. The Client information and the Address information are in two different tables in the database. Below is the code I tried, but it odes not work". I am getting the following error message:

    The INSERT statement conflicted with the FOREIGN KEY constraint "FK_clients_coor_clients1". The conflict occurred in database "Mammouth", table "dbo.Clients", column 'ClientID'.

    I believe that that ClientID is not getting set in the nested FormView. I guess the One-to-One relationship should be taking care of transfering the ClientID to the second FormView once the Client has been inserted, but I am not sure how it is doing it. Maybe I did not provide all the necessary parameters to the relationship.

    Here is the code:

    <asp:FormView ID="ClientFormView" runat="server" DefaultMode="Insert"
        DataSourceID="ClientDataSource" DataKeyNames="ClientID">
         <InsertItemTemplate>
           ...SNIP...
            <asp:FormView ID="ClientCoorFormView" runat="server" DefaultMode="Insert"
                DataSourceID="ClientCoorDataSource" DataKeyNames="CoorClientID">
                <InsertItemTemplate>
                    ...SNIP...
                </InsertItemTemplate>
            </asp:FormView>

            <data:OneToOneViewRelationship ID="ClientCoorRelationship" runat="server">
                <PrimaryMember ID="PrimaryMember" runat="server"
                    DataSourceID="ClientDataSource"
                    EntityKeyName="ClientID" />
                <ReferenceMember ID="ReferenceMember" runat="server"
                    DataSourceID="ClientCoorDataSource"
                    ViewControlID="ClientCoorFormView"
                    EntityKeyName="CoorClientID"/>
            </data:OneToOneViewRelationship>

            <data:ClientCoorDataSource ID="ClientCoorDataSource" runat="server">
            </data:ClientCoorDataSource>
        </InsertItemTemplate>
    </asp:FormView>

    <data:ClientDataSource ID="ClientDataSource" runat="server">
    </data:ClientDataSource>

    Best regards,

    JF

    • Post Points: 5
  • 09-17-2006 11:42 AM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: One-to-One Relationship and InsertItemTemplate

    Update: There was a bug and Bobby is currently fixing it.

    Cheers!

    JF
    • Post Points: 35
  • 09-17-2006 12:43 PM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: One-to-One Relationship and InsertItemTemplate

    This has been committed to the trunk in SVN rev 369.

    Thanks.

    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 35
  • 09-29-2006 3:18 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: One-to-One Relationship and InsertItemTemplate

    Hi Bobby!

    I might have found another bug related to the One-To-One Relationship. I have a scenario similar to the previous one, except for one thing: TheClient already exists, so I have a FormView with an ItemTemplate for the client and another FormView with an InsertItemTemplate for the Address. In this case, the code you added to fix the previous bug is not executed and the ClientId FK in the Coordinates table does not get set. Here is the aspx code:

    <asp:FormView ID="ClientFormView" runat="server" DefaultMode="ReadOnly"
        DataSourceID="ClientDataSource" DataKeyNames="ClientID">
         <ItemTemplate>
           ...SNIP...
            <asp:FormView ID="ClientCoorFormView" runat="server" DefaultMode="Insert"
                DataSourceID="ClientCoorDataSource" DataKeyNames="CoorClientID">
                <InsertItemTemplate>
                    ...SNIP...
                </InsertItemTemplate>
            </asp:FormView>

            <data:OneToOneViewRelationship ID="ClientCoorRelationship" runat="server">
                <PrimaryMember ID="PrimaryMember" runat="server"
                    DataSourceID="ClientDataSource"
                    EntityKeyName="ClientID" />
                <ReferenceMember ID="ReferenceMember" runat="server"
                    DataSourceID="ClientCoorDataSource"
                    ViewControlID="ClientCoorFormView"
                    EntityKeyName="CoorClientID"/>
            </data:OneToOneViewRelationship>

            <data:ClientCoorDataSource ID="ClientCoorDataSource" runat="server">
            </data:ClientCoorDataSource>
        </ItemTemplate>
    </asp:FormView>

    Also, does the second FormView have to be nested inside the first one in this case? And what about the Relationship; does it matter where it is declared?

    Happy Friday!

    JF

    • Post Points: 35
  • 09-29-2006 10:30 PM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: One-to-One Relationship and InsertItemTemplate

    Please add ForeignKeyName="ClientID" to your ReferenceMember and see if that works.

    The second FormView does not need to be nested inside of the first, but the OneToOneViewRelationship control needs to be inside of the same NamingContainer as the FormView so that it can be found with the ViewControlID property.

    Hope that helps.

    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 35
  • 10-04-2006 5:01 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: One-to-One Relationship and InsertItemTemplate

    Hi Bobby!

    As mentioned in a private message, in the real code I am using, ForeignKeyName="ClientID" is there, and the probleme was elsewhere. However, now that I have fixed everything, it is still not working. When I do a trace, all the object properties are set in ExecuteInsert() except for clientID.

    Here  an excerpt of the code I am using. Sorry if it is a bit long. I wanted to make sure that all the necessary information was there. Please note that I have two FormViews: The first one in Edit Mode (The Client already exists in the database; it could also be in RaedOnly Mode). And the second one in Insert Mode. I set a One-To-One Relationship so that the ClientID is updated when inserting the ClientModule, but it does not seem to wrok properly. ClientModule.ClientID stays empty. At this point, I suspect there is really a bug... or maybe it is me who missed something.


        <asp:FormView ID="ClientFormView" runat="server" DefaultMode="Edit"

            DataSourceID="ClientDataSource" DataKeyNames="ClientID">

            <EditItemTemplate>

                     ...[SNIP]...
     

            </EditItemTemplate>

        </asp:FormView>

        <asp:FormView ID="ClientModuleFormView" runat="server" DefaultMode="Insert"

            DataSourceID="ClientModuleDataSource" DataKeyNames="ClientModuleID"

            OnItemInserting="OnModuleAjouterFormViewInserting"

        >

            <InsertItemTemplate>

                                    ...[SNIP]...
     

                            <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Ajouter" />

                            <asp:Button ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Annuler" />

                ...[SNIP]...          

            </InsertItemTemplate>

        </asp:FormView>

        <data:OneToOneViewRelationship ID="ClientClientModuleRelationship" runat="server">

            <PrimaryMember ID="PrimaryMember" runat="server"

                DataSourceID="ClientDataSource"

                ViewControlID="ClientFormView"

                EntityKeyName="ClientID" />

            <ReferenceMember ID="ReferenceMember" runat="server"

                DataSourceID="ClientModuleDataSource"

                ViewControlID="ClientModuleFormView"

                EntityKeyName="ClientModuleID"

                ForeignKeyName="ClientID"/>

        </data:OneToOneViewRelationship>

        <data:ClientDataSource ID="ClientDataSource" runat="server"

            EnableDeepLoad="false" SelectMethod="GetByClientID">

            <Parameters>

                <asp:QueryStringParameter Name="ClientID" QueryStringField="id" Type="String" />

            </Parameters>

        </data:ClientDataSource>

        <data:ClientModuleDataSource ID="ClientModuleDataSource" EnableDeepLoad="false"

            OnInserting="ClientModuleInserting" runat="server">

        </data:ClientModuleDataSource>

    Best regards,

    JF

     

    • Post Points: 35
  • 10-04-2006 7:15 PM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: One-to-One Relationship and InsertItemTemplate

    Have you tried stepping through the OneToOneViewRelationship.UpdateRelationships method to see if the primary key value is being retrieved from the PrimaryMember correctly?

    Thanks. 


    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 60
  • 10-04-2006 7:26 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: One-to-One Relationship and InsertItemTemplate

    Hi Bobby!

    I set a breakpoint at the first line of UpdateRelationships(), but the page executed without stopping. It seems that this function does not get called:

            protected override void UpdateRelationships(Object entity)
            {
    ***            FormView formView = ReferenceMember.ViewControl as FormView;

                    ...

    Is it because I have a FormView in Edit Mode and another one in Insert Mode?

    Cheers!

    JF

     

    • Post Points: 5
  • 10-04-2006 8:16 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 188
    • Points 4,726

    Re: One-to-One Relationship and InsertItemTemplate

    Hi Bobby!

    I did a bit more of tracing and I discovered the following: UpdateRelationships() is only called if an Update or an Insert takes place. This is not the case for my Client object. I load the Client object into a FormView just so that I can display his name and to get the value of the ClientID. The FormView could be in ReadOnly mode, but it is in Edit Mode. Even then, no Update takes place, because there is nothing to update I guess. Because of this, UpdateRelationShips does not get called. Do you see what I mean?

    Cheers!

    JF

     

    • Post Points: 5
Page 1 of 1 (9 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems