Hi, I have the following to display nested comments on my blog which currently uses two tables blogComments and blogNestedComments.
Everything works fine but I'd like to know if somehow I could use just one table to accomplish the same. It would help me on the backend managing the data.
codebehind:
protected void Page_Load(object sender, EventArgs e)
{
GetComments();
}
#region get comments
protected void GetComments()
{
// get required elements
int article = int.Parse(Request.QueryString["article"]);
// get data
SqlDataAdapter cmd = new SqlDataAdapter("SELECT blogComments.commenterName, blogComments.commenterEmail, blogComments.commenterWebSite, blogComments.commenterComment, blogComments.commentDate, blogComments.blogCommentId FROM blogComments INNER JOIN blog ON blogComments.blogId = blog.blogId WHERE(blogComments.blogId = " + article + ") AND (blogComments.commentApproved = 1) AND (blog.blogPublishNow = 1) AND (blog.blogPublishDate <= GETDATE());SELECT * FROM [blogNestedComments] WHERE blogId =" + article + "", new SqlConnection(ConfigurationManager.ConnectionStrings["dbMyCMSConnectionString"].ConnectionString));
// add tables to dataset
DataSet ds = new DataSet();
cmd.Fill(ds);
ds.Relations.Add(new DataRelation("nestThem", ds.Tables[0].Columns["blogCommentId"], ds.Tables[1].Columns["blogCommentId"]));
// repeater1 datasource
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
#endregion
#region Databind repeaters
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView dv = e.Item.DataItem as DataRowView;
if (dv != null)
{
Repeater Repeater2 = e.Item.FindControl("Repeater2") as Repeater;
if (Repeater2 != null)
{
Repeater2.DataSource = dv.CreateChildView("nestThem");
Repeater2.DataBind();
}
}
}
#endregion
aspx:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<div class="commentwrap">
<div class="commentsTitleArea">
<span class="commentCounter"><%# Convert.ToInt32(Container.ItemIndex) + 1%>. </span> <img src="../images/decoy-icon-16px.png" alt="Comment by..." title="Comment by..." class="blogCommentIcon" /><a href='<%# Eval("commenterWebsite")%>' target="_blank" rel="nofollow"><%# " " + Eval("commenterName")%></a> <%# Eval("commentDate")%>
</div>
<div class="commentText">
<%# Eval("commenterComment") %>
<div><span class="btnCommentReply"><a href='<%# "article.aspx?article=" + Request.QueryString["article"] + "&cid=" + Eval("blogCommentId") + "#comment" %>'>REPLY</a></span></div>
</div>
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<div class="commentwrap" style="margin:0px 20px 10px 50px;">
<div class="commentsTitleArea">
<span class="commentCounter"><%# Convert.ToInt32(Container.ItemIndex) + 1%>. </span> <img src="../images/decoy-icon-16px.png" alt="Comment by..." title="Comment by..." class="blogCommentIcon" /><a href='<%# Eval("commenterWebsite")%>' target="_blank" rel="nofollow"><%# " " + Eval("commenterName")%></a> <%# Eval("commentDate")%>
</div>
<div class="commentText">
<%# Eval("commenterComment") %>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater> Thank you!