S
Sam Collett
I am looping through a table to get the number of replies to a post on
a discussion system. 'posID' is the primary key (i.e. thread id),
'posParent' is the parent thread. However, it only seems to go down one
level (i.e. direct replies, but not replies of replies)
private int ReplyCount(int Id, int replies) {
SqlConnection SqlConn = new SqlConnection(SqlConnString);
// get child posts SQL statement
string SQL = "SELECT * FROM post WHERE posParent = @posId";
SqlDataAdapter SqlDa = new SqlDataAdapter();
// select command
SqlDa.SelectCommand = new SqlCommand(SQL, SqlConn);
// add sql parameter to select command
SqlDa.SelectCommand.Parameters.Add(new SqlParameter("@posId", Id));
DataSet childPosts = new DataSet();
SqlDa.Fill(childPosts, "Child");
// increment replies counter
replies+=(int)childPosts.Tables["Child"].Rows.Count;
if (childPosts.Tables["Child"].Rows.Count > 0) {
for (int i = 0; i < childPosts.Tables["Child"].Rows.Count; i++)
{
ReplyCount((int)childPosts.Tables["Child"].Rows["posID"],replies);
}
}
return replies;
}
a discussion system. 'posID' is the primary key (i.e. thread id),
'posParent' is the parent thread. However, it only seems to go down one
level (i.e. direct replies, but not replies of replies)
private int ReplyCount(int Id, int replies) {
SqlConnection SqlConn = new SqlConnection(SqlConnString);
// get child posts SQL statement
string SQL = "SELECT * FROM post WHERE posParent = @posId";
SqlDataAdapter SqlDa = new SqlDataAdapter();
// select command
SqlDa.SelectCommand = new SqlCommand(SQL, SqlConn);
// add sql parameter to select command
SqlDa.SelectCommand.Parameters.Add(new SqlParameter("@posId", Id));
DataSet childPosts = new DataSet();
SqlDa.Fill(childPosts, "Child");
// increment replies counter
replies+=(int)childPosts.Tables["Child"].Rows.Count;
if (childPosts.Tables["Child"].Rows.Count > 0) {
for (int i = 0; i < childPosts.Tables["Child"].Rows.Count; i++)
{
ReplyCount((int)childPosts.Tables["Child"].Rows["posID"],replies);
}
}
return replies;
}