Recursive counter

Discussion in 'ASP .Net' started by Sam Collett, Nov 3, 2004.

  1. Sam  Collett

    Sam Collett Guest

    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;


    }
     
    Sam Collett, Nov 3, 2004
    #1
    1. Advertising

  2. Sam  Collett

    bruce barker Guest

    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["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:...
    > 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;
    >
    >
    > }
    >
     
    bruce barker, Nov 3, 2004
    #2
    1. Advertising

  3. Sam  Collett

    Sam Collett Guest

    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["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?
     
    Sam Collett, Nov 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam  Collett

    Recursive counter

    Sam Collett, Nov 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    419
    Marina
    Nov 3, 2004
  2. The Eeediot
    Replies:
    3
    Views:
    2,281
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  3. Petterson Mikael

    help counter recursive template

    Petterson Mikael, Feb 9, 2006, in forum: XML
    Replies:
    2
    Views:
    499
    Joe Kesselman
    Feb 11, 2006
  4. George2
    Replies:
    1
    Views:
    832
    Alf P. Steinbach
    Jan 31, 2008
  5. Thibaut Barrère

    Recursive line counter (review?)

    Thibaut Barrère, Mar 13, 2006, in forum: Ruby
    Replies:
    3
    Views:
    97
    Robert Klemme
    Mar 13, 2006
Loading...

Share This Page