Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Recursive counter

Reply
Thread Tools

Recursive counter

 
 
Sam Collett
Guest
Posts: n/a
 
      11-03-2004
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;


}

 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      11-03-2004
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" <> wrote in message
news: 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;
>
>
> }
>



 
Reply With Quote
 
 
 
 
Sam Collett
Guest
Posts: n/a
 
      11-04-2004
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page File counter and Private Bytes Counter George2 C++ 1 01-31-2008 09:27 AM
Recursive line counter (review?) Thibaut Barrère Ruby 3 03-13-2006 04:37 PM
help counter recursive template Petterson Mikael XML 2 02-11-2006 01:48 AM
Session("counter") vs. ViewState("counter")...a newbie question The Eeediot ASP .Net 3 12-22-2004 09:31 PM
Recursive counter Sam Collett ASP .Net 1 11-03-2004 05:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57