Search code examples
c#asp.netsqlparent-childrepeater

Error "parent key and child key are identical" using single table as Parent and Child tables for Nested Repeater


I am working with an outer and inner repeater. The first lists "company name". Second lists "tasks" completed for that company. For each company there are many tasks. This is my first time working with nested repeaters.

The issue is, I am using one table ("Tracker"). Column CompanyName comes from Table1("Tracker") as does Task, every example I have worked with uses two tables. When creating the relation I get error "parent key and child key are identical"

    ds.Relations.Add("myrelation", _
    ds.Tables("Tracker").Columns("CompanyName"), _
    ds.Tables("Tracker").Columns("CompanyName"))
    PRepeater.DataSource = ds.Tables("Tracker")

Is there a way around this? Another way to do it? Other than make another Database? VB or C# is fine.

Here is the entire Sub that I call on a btn click and the aspx section if that helps.

    Protected Sub bindParent()

    conn = New System.Data.SqlClient.SqlConnection
    conn.ConnectionString = ConfigurationSettings.AppSettings("connString")
    If conn.State = ConnectionState.Closed Then
        conn.Open()
    End If
    Dim cmd1 As New SqlDataAdapter("Select * from Tracker", conn)
    Dim ds As New DataSet()
    cmd1.Fill(ds, "Tracker")

    Dim cmd2 As New SqlDataAdapter("Select * from Tracker", conn)
    cmd2.Fill(ds, "Tracker")
    ds.Relations.Add("myrelation", _
    ds.Tables("Tracker").Columns("CompanyName"), _
    ds.Tables("Tracker").Columns("CompanyName"))
    PRepeater.DataSource = ds.Tables("Tracker")

    PRepeater.DataSource = ds.Tables("Tracker")
    Page.DataBind()

    conn.Close()

    End Sub

aspx

   <asp:Panel ID="pnlCompany" runat="server" visible="false"/>         
        <asp:Repeater ID="PRepeater" runat="server">
            <ItemTemplate>
            <tr>
                <b><%# DataBinder.Eval(Container.DataItem, "CompanyName") %></b>
            </tr>

                <asp:Repeater ID="childRepeater" runat="server" DataSource='<%# Container.DataItem.Row.GetChildRows("myrelation") %>'>
                    <ItemTemplate>
                        <%#Container.DataItem("Task")%><br />
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>

*This is the example I worked from http://support.microsoft.com/default.aspx?scid=kb;EN-US;326338


Solution

  • Solved this gave me the output I wanted and no more error message

    Dim cmd1 As New SqlDataAdapter("Select distinct CompanyName from Tracker", conn)
    Dim ds As New DataSet()
    cmd1.Fill(ds, "Company")
    
    Dim cmd2 As New SqlDataAdapter("Select * from Tracker", conn)
    cmd2.Fill(ds, "Tracker")
    
    ds.Relations.Add("myrelation", _
    ds.Tables("Company").Columns("CompanyName"), _
    ds.Tables("Tracker").Columns("CompanyName"))
    PRepeater.DataSource = ds.Tables("Company")
    PRepeater.DataBind()
    
    conn.Close()
    

    *moving solution from question to answer