0

この記事は、2つのテーブルのネストされたデータバインディングを実行するためのガイドとして使用しています。「コメント」「CommentOtherAuthor」:http ://support.microsoft.com/kb/306154 。1つのコメントに多くの作者がいる可能性があります。私が持っているコードはここにあります:

.ASPX:

<asp:Repeater ID="rptComments" runat="server">
    <ItemTemplate>
            <div class="comment-data">
                <h3 class="item">Submitted to <%# GetPageDetails(Eval("nodeid")) %> article on <%# Eval("created") %></strong></h3>
                <p class="item"><strong>Name</strong> <%# Eval("firstname") %> <%# Eval("surname") %></p>
                <p class="item"><strong>Occupation</strong> <%# Eval("occupation") %></p>
                <p class="item"><strong>Affiliation</strong> <%# Eval("affiliation") %></p>
                <p class="item"><strong>Email</strong> <a href='mailto:<%# Eval("email") %>'><%# Eval("email") %></a> <em>Publish email: <%# Eval("publishemail") %></em></p>
                <p class="item"><strong>Competing interests?</strong> <%# Eval("competingintereststext") %>&nbsp;</p>
                <p class="item"><strong>eLetter title</strong> <%# Eval("title") %></p>
                <p><%# Eval("comment").ToString().Replace("\n", "<br/>")%></p>

                <div class="additional-authors">
                    <h3>Additional authors</h3>
                    <asp:Repeater id="rptAdditionalAuthors" runat="server" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' >
                        <ItemTemplate>
                            <%# DataBinder.Eval(Container.DataItem, "[\"firstname\"]")%><br>    
                        </ItemTemplate>
                    </asp:Repeater>
                </div>
            </div>
    </ItemTemplate>
</asp:Repeater>

コードビハインド:

    private void BindData()
    {

        SqlConnection cnn = new SqlConnection(GlobalSettings.DbDSN);
        SqlDataAdapter cmd1 = new SqlDataAdapter(string.Format("select * from Comment {0} order by created desc", Filter), cnn);

        //Create and fill the DataSet.
        DataSet ds = new DataSet();
        cmd1.Fill(ds, "comments");

        //Create a second DataAdapter for the additional authors table.
        SqlDataAdapter cmd2 = new SqlDataAdapter("select * from CommentOtherAuthor", cnn);
        cmd2.Fill(ds, "additionalAuthors");

        //Create the relation between the comments and additional authors tables.
        ds.Relations.Add(
            "myrelation",
            ds.Tables["Comment"].Columns["id"],
            ds.Tables["CommentOtherAuthor"].Columns["commentid"]
        );

        //Bind the Authors table to the parent Repeater control, and call DataBind.
        rptComments.DataSource = ds.Tables["additionalAuthors"];
        rptComments.DataBind();
    }

ただし、これを実行すると、行にSystem.NullReferenceExceptionがスローされますds.Relations.Add(

私はここで私の深さをはるかに超えているので、これを修正するためにどこから始めればよいのか本当にわかりません。

誰かがこれを機能させる方法をアドバイスできますか?

ありがとう。

4

1 に答える 1

0

とった!テーブルを結合する実際の外部キー制約はありませんでした。これで、これが機能するようになりました。

于 2012-06-05T16:44:17.273 に答える