我正在使用这篇文章作为对我的两个表'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") %> </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();
}
但是,在运行它时会在行ds.Relations.Add(
上抛出 System.NullReferenceException
我真的不知道从哪里开始解决这个问题,因为我已经离开了这里。
有人可以建议如何使这个工作吗?
感谢。
答案 0 :(得分:0)
知道了!加入表时没有实际的外键约束。有了这个,这现在有效。