ASP.NET数据集添加关系空引用异常
我将本文用作对我的两个表“Comment”“CommentOtherAuthor”进行嵌套数据绑定的指南:。一条评论可能有许多作者。我的代码如下: .ASPX: 但是,运行此命令时,它会在ds.Relations.Add行上抛出System.NullReferenceException 我真的不知道从哪里开始解决这个问题,因为我已经超出了我的深度 有谁能建议如何让它工作ASP.NET数据集添加关系空引用异常,asp.net,data-binding,nullreferenceexception,Asp.net,Data Binding,Nullreferenceexception,我将本文用作对我的两个表“Comment”“CommentOtherAuthor”进行嵌套数据绑定的指南:。一条评论可能有许多作者。我的代码如下: .ASPX: 但是,运行此命令时,它会在ds.Relations.Add行上抛出System.NullReferenceException 我真的不知道从哪里开始解决这个问题,因为我已经超出了我的深度 有谁能建议如何让它工作 谢谢。明白了!连接表时没有实际的外键约束。有了这一点,现在就可以了 <asp:Repeater ID="rptComme
谢谢。明白了!连接表时没有实际的外键约束。有了这一点,现在就可以了
<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();
}