CodeSmith Community
Your Code. Your Way. Faster!

Another n00b here - please help! *code inside*

Latest post 09-14-2007 12:23 PM by bgjohnso. 3 replies.
  • 09-13-2007 6:58 AM

    • kushal18
    • Not Ranked
    • Joined on 08-27-2007
    • Posts 7
    • Points 165

    Another n00b here - please help! *code inside*

    First of all, thanks and great work to all for this fantastic tool!

    I've been using .nettiers for about 2 months now and recently came across this problem.

    Scenario

    I have a table 'Person' and another 'RelatedPersons'. A 'Person' can have 0 or more 'RelatedPersons'.

    The 'RelatedPersons' table is made up of two fields, both foreign key of 'PersonId' from the 'Person' table.

    The Code

    This is the code which can be used to replicate the scenario described above. 

    GO
    CREATE TABLE [dbo].[Person](
        [PersonID] [int] IDENTITY(1,1) NOT NULL,
        [PersonName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    PRIMARY KEY CLUSTERED
    (
        [PersonID] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]

    GO
    CREATE TABLE [dbo].[RelatedPersons](
        [PersonID] [int] NOT NULL,
        [RelatedPersonID] [int] NOT NULL,
     CONSTRAINT [PK_RelatedPersons] PRIMARY KEY CLUSTERED
    (
        [PersonID] ASC,
        [RelatedPersonID] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]

    GO
    ALTER TABLE [dbo].[RelatedPersons]  WITH CHECK ADD  CONSTRAINT [FK_RelatedPersons_Person] FOREIGN KEY([PersonID])
    REFERENCES [dbo].[Person] ([PersonID])
    GO
    ALTER TABLE [dbo].[RelatedPersons]  WITH CHECK ADD  CONSTRAINT [FK_RelatedPersons_Person1] FOREIGN KEY([RelatedPersonID])
    REFERENCES [dbo].[Person] ([PersonID])

    Problem

    On the 'Person Edit' page, when I want to  'Add a new person...' and link it at the same time to another person ('RelatedPerson'), the link does not seem to be saved to the database.

    The 'RelatedPersons' page also does not show an entry to that table. The 'Person' table however gets populated wihout any problem. 

     

    Can anyone please advise me as to what I'm doing wrong and if there's a way around it?

     Thanks in advance Wink
     

    • Post Points: 35
  • 09-13-2007 4:42 PM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 764
    • Points 22,530

    Re: Another n00b here - please help! *code inside*

    Reply |Contact |Answer

    It always amazes how many different permutations of relationships that pop up in people's databases.  I recreated the schema above and saw a couple of errors right off.  First of all, it generated two sets of CheckBox lists (one for each FK pointing back to the Person table).  Both sets were identical and I had to remove one in order to even compile the application.  Did you see this as well?  Next, I noticed that the PersonDataSource generated in the PersonFields.ascx had the same id as the PersonDataSource in the PersonEdit.aspx page.  Also, the LinkMember item in the ManyToManyRelationship control had the EntityKeyName and ForeigtnKeyName set to the same value of PersonId (ForeignKeyName should be RelatedPersonID).

     In the end, the relevant section in PersonField.ascx looked like the following (I have underlined the changed parts):

    <tr>
                    <td valign="top" class="literal">Person:</td>
                    <td>
                        <asp:CheckBoxList ID="PersonList" runat="server"
                            DataSourceID="PersonReferenceDataSource"
                            DataTextField="PersonName"
                            DataValueField="PersonID"
                            RepeatColumns="4"
                        />                
                        <data:PersonDataSource ID="PersonReferenceDataSource" runat="server" SelectMethod="GetAll"/>
                        
                        <data:RelatedPersonsDataSource ID="RelatedPersonsDataSource" runat="server" SelectMethod="GetByPersonID" >
                            <Parameters>                            
                                <asp:QueryStringParameter Name="PersonID" QueryStringField="PersonID" Type="String" />
                            </Parameters>
                        </data:RelatedPersonsDataSource>    
                        
                        <data:ManyToManyListRelationship ID="RelatedPersonsRelationship" runat="server">
                            <PrimaryMember ID="PrimaryMember1" runat="server" DataSourceID="PersonDataSource" EntityKeyName="PersonID" />
                            <LinkMember ID="LinkMember1" runat="server" DataSourceID="RelatedPersonsDataSource" EntityKeyName="PersonID" ForeignKeyName="RelatedPersonID" />
                            <ReferenceMember ID="ReferenceMember1" runat="server" DataSourceID="PersonReferenceDataSource" ListControlID="PersonList" EntityKeyName="PersonID" />
                        </data:ManyToManyListRelationship>                    
                    </td>
                </tr>            
    

    I have attached a patch that will generate the code above and hopefully fix your issue.  Can you apply this patch to your code base and let me know how it works?

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 60
  • 09-13-2007 7:37 PM In reply to

    • kushal18
    • Not Ranked
    • Joined on 08-27-2007
    • Posts 7
    • Points 165

    Re: Another n00b here - please help! *code inside*

    Hi mate,

    I've tried it with the changes you provided: tried, tested and satisfied! It now works as expected. Yes

    Just for the record, I was also experiencing the similar issues as you did, namely duplicate fields and duplicate names.

    Now for some regression testing... Wink

    • Post Points: 35
  • 09-14-2007 12:23 PM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 764
    • Points 22,530

    Re: Another n00b here - please help! *code inside*

    Glad to help.  This has been committed in SVN 636.

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

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