0

我使用这篇文章作为对我的两个表 'Comment' 'CommentOtherAuthor' 进行嵌套数据绑定的指南:http: //support.microsoft.com/kb/306154。一条评论可能有很多作者。我的代码在这里:

.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.NullReferenceExceptionds.Relations.Add(

我真的不确定从哪里开始解决这个问题,因为我在这里已经超出了我的深度。

谁能建议如何使它工作?

谢谢。

4

1 回答 1

0

知道了!没有实际的外键约束加入这些表。有了这些,现在就可以了。

于 2012-06-05T16:44:17.273 回答