1

I want to get a list of values, After I get one value I pass the value and call the same method.

if the structure is like

60
|
61
|
63
|
64

the code works fine and returns all the value when I pass 60.

but when the structure is like

    60 
   |  |
  61  63
   |
  62

so here the parent id of 61 and 63 is 60.

Then the Org id passed after the first execution is 63, since 61 and 63 are read at the same time and 63 is the last read orgid, so it passes 63 to the next loop and it returns 0 and the loop end. therefore no execution is done using orgid 61. so 62 is missed. what mistake am I doing here. when a reader is reading 2 values in the same query then the second value read is passed. I want to pass all the value read in the query.

private DataSet BindGridView(int id, List<int> userids)
{
    string GetOrgIDs = "select OrganisationID from tbl_organisation where ParentID =@ParentID";
    MySqlParameter[] paramet = new MySqlParameter[1];
    paramet[0] = new MySqlParameter("@ParentID", MySqlDbType.Int32);
    paramet[0].Value = id;
    int Orgid = 0;
    MySqlDataReader reader = server.ExecuteReader(CommandType.Text, GetOrgIDs, paramet);
    while (reader.Read())
    {
        Orgid = Convert.ToInt32(reader["OrganisationID"]);
        userids.Add(Orgid);
    }
    reader.Close();
    if (Orgid != 0)
    {
        BindGridView(Orgid, userids);
    }
}

I call the method from page load

 List<int> OrgIDs = new List<int>();
                OrgIDs.Add(60);
                BindGridView(60, OrgIDs);

I am using Mysql db

3 Answers 3

4

Your recursive call will need to sit in the while loop in order to call each Orgid, however this is going to result in multiple simultaneous reads connect to your database to resources could be an issue.

while (reader.Read())
{
    Orgid = Convert.ToInt32(reader["OrganisationID"]);
    userids.Add(Orgid);
    if (Orgid != 0)
    {
        BindGridView(Orgid, userids);
    }
}
reader.Close();
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, here if 60 is passed, first it gets 61 and then 61 is passed to the method and I get 62, but wouldnt I miss the 63 and I will also get There is already an open DataReader associated with this Connection which must be closed first. error
2

You are recursing with a parent of the last id found. Thus you ignore any possible children of earlier values.

Add the values you found to both that list parameter and a (new) local list. Then loop over that local list and recurse if the value != 0.

This way you will not have multiple resultsets open.

private DataSet BindGridView(int id, List<int> userids)
{
    string GetOrgIDs = "select OrganisationID from tbl_organisation where ParentID =@ParentID";
    MySqlParameter[] paramet = new MySqlParameter[1];
    paramet[0] = new MySqlParameter("@ParentID", MySqlDbType.Int32);
    paramet[0].Value = id;
    List<int> children = new List<int>(); // new local list
    MySqlDataReader reader = server.ExecuteReader(CommandType.Text, GetOrgIDs, paramet);
    while (reader.Read())
    {
        int orgid = Convert.ToInt32(reader["OrganisationID"]);
        userids.Add(orgid);
        children.Add(orgid);  // also add to local list
    }
    reader.Close();

    foreach(int child in children)
    if (child != 0)
    {
        BindGridView(child, userids);
    }
}

Comments

0

This loop could run 100x times.

while (reader.Read())
{
    Orgid = Convert.ToInt32(reader["OrganisationID"]);
    userids.Add(Orgid);
}
reader.Close();

After all records are looped OrdId only has the last value.

if (Orgid != 0)
{
    BindGridView(Orgid, userids);
}

BindGridView(Orgid, userids); is only being called on the last OrgId in the reader.read() loop.

I would think it would only call BindGridView on 63 (depending on data order) and skip 61 and any others before it.

1 Comment

Sorry, I should have added some example code to correct. There are some other good examples from Andy and Hans already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.