473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Recursive counter

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[i]["posID"],replies);
}
}
return replies;
}

Nov 18 '05 #1
2 1512
you are not including the child count, try:

for (int i = 0; i < childPosts.Tables["Child"].Rows.Count; i++)
{
replies +=
ReplyCount((int)childPosts.Tables["Child"].Rows[i]["posID"],replies);
}
note: this is a very slow way to count. you should write a sql stored proc
to do it.

-- bruce (sqlwork.com)
"Sam Collett" <sa*********@gmail.com> wrote in message
news:10**********************@f14g2000cwb.googlegr oups.com...
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[i]["posID"],replies);
}
}
return replies;
}

Nov 18 '05 #2
Seems I don't need the second parameter (replies):
private int ReplyCount(int Id)

Loop then becomes:
if (childPosts.Tables["Child"].Rows.Count > 0) {
for (int i = 0; i < childPosts.Tables["Child"].Rows.Count; i++)
{
replies +=
ReplyCount((int)childPosts.Tables["Child"].Rows[i]["posID"]);
}
}

Not the most efficient way, but I don't expect too many replies (less
than 20). How would this be done via a stored procedure, and would it
really make a difference?

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
10
by: action! | last post by:
this is my C Programming homework, It wants me to input 10! and output the follow result 10! ¡õ 10 * 9! ¡õ 9 * 8! ..................
1
by: Sam Collett | last post by:
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...
2
by: Petterson Mikael | last post by:
Hi, I call the sequenceNameString template with: <xsl:when test="child::*"> <xsl:call-template name="sequenceNameString"> <xsl:with-param name="sequenceName" select="@name"/> <xsl:with-param...
3
by: Ameen | last post by:
I am very new to programming and VB.net, and I wrote the code below, but it seem to go on an infinite loop. Please tell me what I am doing wrong. Private Sub search(ByVal indexstart) Pos1 =...
5
by: monmonja | last post by:
Hi i'm new to xsl and i have been using smarty php templating but its just so hard to read codes in smarty/php/flash than xml/xsl/flash, i rather sacrifice speed then not being able to read code...
3
by: pantagruel | last post by:
Hi, I have the following: public static void RunBatch() { if (OnDemandRunning) { //If OnDemand is Running we will do nothing } else{
9
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
2
by: felciano | last post by:
Hello -- I am trying to use XSL to process Amazon wishlist data to sort the results by type (Apparel, then Books, then DVDs, etc). Amazon's web services chunk up results in multiple pages of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.