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

    Marina Guest

    That is because you are passing in replies by value - so it gets upped, but
    does not reflect in the ReplyCount that called it. Try making it a ref
    parameter.

    "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;
    >
    >
    > }
    >
     
    Marina, Nov 3, 2004
    #2
    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:
    2
    Views:
    524
    Sam Collett
    Nov 4, 2004
  2. The Eeediot
    Replies:
    3
    Views:
    2,264
    =?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:
    821
    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:
    95
    Robert Klemme
    Mar 13, 2006
Loading...

Share This Page