473,382 Members | 1,480 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,382 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 1511
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.