locked
simply display data in a hierarchical view with main group ,sub group and a child group RRS feed

  • Question

  • User-1027332312 posted

    this is the aspx part of the code

    <div class="col-lg-6 col-md-6" style="width: 49%; float: left; background-color: ghostwhite; margin-right: 1%; height: 50%">
    <h2 style="background-color: gainsboro; text-align: center; margin-top: 0px; width: 100%; height: 50px; padding-top: 10px;"><strong>Assets</strong></h2>
    <ul style="list-style-type: none">
    <% if (dt.Rows.Count > 0)
    {

    for (int i = 0; i < dt.Rows.Count; i++)
    {
    Load_Asset_Details(Convert.ToInt32(dt.Rows[i]["id"]), dt.Rows[i]["name"].ToString());
    Load_Asset_Ledgers(dt4.Rows[i]["name"].ToString());

    %>
    <li class="droplink activeopen"><span style="align-content: center;"><strong><%=dt.Rows[i]["name"].ToString() %></strong><span class="arrow"></span></span>

    <ul style="list-style-type: none">
    <%if (dt4.Rows.Count > 0)
    {
    for (int j = 0; j < dt4.Rows.Count; j++)
    {
    //Load_Asset_Ledgers(dt4.Rows[j]["name"].ToString());
    %>
    <li>
    <span style="align-content: center;"><strong><%=dt4.Rows[j]["name"].ToString() %></strong><span class="arrow"></span></span>
    <ul>
    <%if (d11.Rows.Count > 0)
    {
    //Load_Asset_Ledgers(d11.Rows[j]["name"].ToString());
    for (int k = 0; k <= d11.Rows.Count-1; k++)
    {

    %>

    <li>
    <span><%=d11.Rows[k]["name"].ToString() %></span>
    </li>
    <%}
    } %>
    </ul>

    </li>
    <%}
    } %>
    </ul>
    </li>

    <%}
    } %>
    </ul>

    </div>

    <div class="col-lg-6 col-md-6" style="width:49%;float:left;border:thick;background-color:ghostwhite;margin-left:1%;height:50%">
    <h2 style="background-color:gainsboro;text-align:center;margin-top:0px;width:100%;height:50px;padding-top:10px;"><strong>Income</strong></h2>
    <ul style="list-style-type:none" >
    <% if (dt1.Rows.Count > 0) {

    for (int i = 0; i < dt1.Rows.Count; i++)
    {
    Load_Income_Details(Convert.ToInt32(dt1.Rows[i]["id"]), dt1.Rows[i]["name"].ToString());


    %>
    <li class="droplink activeopen"> <span style="align-content:center;"><strong><%=dt1.Rows[i]["name"].ToString() %></strong></span>
    <!-- <button class="icon-btn" data-button="btnData" runat="server" style="width:35px;height:20px;background-color:black">+</button>
    <div class="showData">-->
    <ul style="list-style-type: none">
    <%if (dt5.Rows.Count > 0)
    {
    for (int j = 0; j < dt5.Rows.Count; j++)
    {
    %>
    <li>
    <span style="align-content: center;"><strong><%=dt5.Rows[j]["name"].ToString() %></strong><span class="arrow"></span></span>
    </li>
    <%
    }
    }%>
    </ul>

    </li>
    <!--</div>-->
    <%}
    } %>
    </ul>
    </div>


    <div class="col-lg-6 col-md-6" style="width:49%;float:left;border:thick;background-color:ghostwhite;margin-left:1%;height:50%;">
    <h2 style="background-color:gainsboro;text-align:center;margin-top:0px;width:100%;height:50px;padding-top:10px;"><strong>Expenses</strong></h2>
    <ul style="list-style-type:none">
    <% if (dt3.Rows.Count > 0) {

    for (int i = 0; i < dt3.Rows.Count; i++)
    {
    Load_Expens_Details(Convert.ToInt32(dt3.Rows[i]["id"]), dt3.Rows[i]["name"].ToString());


    %>
    <li ><span style="align-content:center;"><strong><%=dt3.Rows[i]["name"].ToString() %></strong></span>
    <ul style="list-style-type: none">
    <%if (dt7.Rows.Count > 0)
    {
    for (int j = 0; j < dt7.Rows.Count; j++)
    {
    %>
    <li>
    <span style="align-content: center;"><strong><%=dt7.Rows[j]["name"].ToString() %></strong><span class="arrow"></span></span>
    </li>
    <%
    }
    }%>
    </ul>
    </li>

    <%}
    } %>
    </ul>

    </div>
    <div class="col-lg-6 col-md-6" style="width:49%;float:left;border:thick;background-color:ghostwhite;margin-right:1%;height:50%">
    <h2 style="background-color:gainsboro;text-align:center;margin-top:0px;width:100%;height:50px;padding-top:10px;"><strong>Liabilities</strong></h2>
    <ul style="list-style-type:none">
    <% if (dt2.Rows.Count > 0) {

    for (int i = 0; i < dt2.Rows.Count; i++)
    {
    Load_Liability_Details(Convert.ToInt32(dt2.Rows[i]["id"]), dt2.Rows[i]["name"].ToString());



    %>
    <li ><span style="align-content:center;"><strong><%=dt2.Rows[i]["name"].ToString() %></strong></span>
    <ul style="list-style-type: none">
    <%if (dt6.Rows.Count > 0)
    {
    for (int j = 0; j < dt6.Rows.Count; j++)
    {
    %>
    <li>
    <span style="align-content: center;"><strong><%=dt6.Rows[j]["name"].ToString() %></strong><span class="arrow"></span></span>
    </li>
    <%
    }
    }%>
    </ul>
    </li>
    <%}
    } %>
    </ul>
    </div>

    here is the code im using,

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    Load_Asset();
    Load_Income();
    Load_Liabilities();
    Load_Expenses();

    }

    //Label1.Text = dt.Rows[0]["name"].ToString();

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.Redirect("Add_Group.aspx?action=s");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
    Response.Redirect("Add_Group.aspx?action=i");
    }
    public void Load_Asset()
    {
    string asset = "Asset";
    dt = db.select("SELECT * FROM tbl_chart_account where account_name='"+ asset + "' and account_id='"+Session["uid"]+"' and company_id='"+Session["Newc"]+"'");
    }


    public void Load_Asset_Details(int id, string name)
    {
    string assett = "Asset";
    if (dt4 != null)
    {
    Load_Asset_Ledgers(name);
    if (dt4.Rows != null)
    {

    if (dt4.Rows.Count > 0)
    {
    Load_Asset_Ledgers(name);
    dt4.Clear();
    }
    }
    }


    dt4 = db.select("SELECT * FROM tbl_chart_item WHERE Sub_group='" + name + "'and account_id='" + Session["uid"] + "' and company_id='" + Session["Newc"] + "' ");
    d11 = db.select("SELECT * FROM tbl_chart_ledger WHERE main_group='" + assett + "' and Sub_group='" + name + "' and account_id='" + Session["uid"] + "' and company_id='" + Session["Newc"] + "' ");

    }
    public void Load_Asset_Ledgers(string name)
    {
    //string assett = "Asset";
    if (d11 != null)
    {
    if (d11.Rows != null)
    {
    if (d11.Rows.Count > 0)
    {
    d11.Clear();
    }
    }
    }


    d11 = db.select("SELECT * FROM tbl_chart_ledger WHERE Sub_group='"+name+ "' and account_id='" + Session["uid"] + "' and company_id='" + Session["Newc"] + "' ");

    }
    public void Load_Income()
    {
    string income = "Income";
    dt1 = db.select("SELECT * FROM tbl_chart_account where account_name='" + income + "' and account_id='" + Session["uid"] + "' and company_id='" + Session["Newc"] + "' ");

    }
    public void Load_Income_Details(int id, string name)
    {
    string incom = "Income";
    if (dt5 != null)
    {
    if (dt5.Rows != null)
    {
    if (dt5.Rows.Count > 0)
    {
    dt5.Clear();
    }
    }
    }


    dt5 = db.select("SELECT * FROM tbl_chart_item WHERE account_name='" + incom + "' and Sub_group='" + name + "' and account_id='" + Session["uid"] + "' and company_id='" + Session["Newc"] + "' ");

    }
    public void Load_Liabilities()
    {
    string liability = "Liability";
    dt2 = db.select("SELECT * FROM tbl_chart_account where account_name='" + liability + "' and account_id='" + Session["uid"] + "' and company_id='" + Session["Newc"] + "' ");

    }
    public void Load_Liability_Details(int id, string name)
    {
    string liab = "Liability";
    if (dt6 != null)
    {
    if (dt6.Rows != null)
    {
    if (dt6.Rows.Count > 0)
    {
    dt6.Clear();
    }
    }
    }


    dt6 = db.select("SELECT * FROM tbl_chart_item WHERE account_name='" + liab + "' and Sub_group='" + name + "' and account_id='" + Session["uid"] + "' and company_id='" + Session["Newc"] + "' ");

    }
    public void Load_Expenses()
    {
    string expens = "Expense";
    dt3 = db.select("SELECT * FROM tbl_chart_account where account_name='" + expens + "'");
    }
    public void Load_Expens_Details(int id, string name)
    {
    string expen = "Expense";
    if (dt7 != null)
    {
    if (dt7.Rows != null)
    {
    if (dt7.Rows.Count > 0)
    {
    dt7.Clear();
    }
    }
    }


    dt7 = db.select("SELECT * FROM tbl_chart_item WHERE account_name='" + expen + "' and Sub_group='" + name + "' and account_id='" + Session["uid"] + "' and company_id='" + Session["Newc"] + "' ");

    }

    1. when running this code i am getting the same child group for all the subgroups and i cant sort out what the real issue is
    Monday, April 1, 2019 10:04 AM

Answers

  • User-893317190 posted

    Hi athulyashaji ,

    You could customize a class which could store data from you table.The node class has a property named ChildNodes which stores members of child group.

    Below is my code.

     
    public class Node {
    public int Level { get; set; }
    public string NodeName { get; set; }
    public List<Node> ChildNodes { get; set; }
    }
    public partial class mutileveldata : System.Web.UI.Page { public Node RootNode = new Node { Level = 1, NodeName = "assets" ,ChildNodes=new List<Node>()}; private static string constr = ConfigurationManager.ConnectionStrings["EntityDb"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand com = new SqlCommand("select name from table1 where account_name = @name", con)) { con.Open(); com.Parameters.AddWithValue("name", "assets"); using (SqlDataReader reader =com.ExecuteReader()) { while (reader.Read()) { // get child node of assets Node mainGroupNode = new Node { Level = 2, NodeName = reader["name"].ToString(),ChildNodes= new List<Node>()}; // add child node to root node RootNode.ChildNodes.Add(mainGroupNode); // start to add the next level using (SqlConnection con1 = new SqlConnection(constr)) { using (SqlCommand com1 = new SqlCommand("select name from maingroup where sub_group = @name", con1)) { con1.Open(); com1.Parameters.AddWithValue("name", mainGroupNode.NodeName); using (SqlDataReader reader1 = com1.ExecuteReader()) { while (reader1.Read()) { // get child node of maingroup Node subgroupnode = new Node { Level = 3, NodeName = reader1["name"].ToString(), ChildNodes = new List<Node>() }; // add subgroup to maingroup mainGroupNode.ChildNodes.Add(subgroupnode); // start to add next level using (SqlConnection con2 = new SqlConnection(constr)) { using (SqlCommand com2 = new SqlCommand("select name from subgroup where sub_item = @name", con2)) { con2.Open(); com2.Parameters.AddWithValue("name", subgroupnode.NodeName); using (SqlDataReader reader2 = com2.ExecuteReader()) { while (reader2.Read()) { // get the child node of subgroup Node sub_item = new Node { Level = 4, NodeName = reader2["name"].ToString() }; // add child node to subgroup subgroupnode.ChildNodes.Add(sub_item); } } } } } } } } } } } } } } }

    After executing the code above, the RootNode will have all the data divided with group.

    Then you could render it in aspx.

     <form id="form1" runat="server">
            <%-- first level --%>
            <% foreach (var item in RootNode.ChildNodes)
                {
                  
                    
                  %>
           
            <div style="padding-left:0px;color:red "><%=item.NodeName %>  </div>
           
                <%
    
                        foreach (var item1 in item.ChildNodes)
                        {%>
             <%-- second level --%>
                               <div style="padding-left:20px;color:blue "><%=item1.NodeName %>  </div>
                        
                                 <%foreach (var item2 in item1.ChildNodes)
                                     {%>
             <%-- third level --%>
                   <div style="padding-left:40px;color:orange "><%=item2.NodeName %>  </div>
                                     <%} %>
    
                        <%}
    
    
    
    
    
                    } %>
        </form>

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 4, 2019 4:02 AM

All replies

  • User-1174608757 posted

    Hi, athulysahaji

    athulyashaji

    main group ,sub group and a child group

    So it is really hard to understand your construct of Group if you just post these codes , so could you please make an explain  which one is the main group and which is sub group. It will help us to understand the whole construct of your groups and solve your problem. 

    Best Regards

    Wei

    Tuesday, April 2, 2019 6:38 AM
  • User-1027332312 posted

    here in case of assets dt isthe table for group and dt4 sub group and d11 child group. Each main group contains corresponding sub group and each sub group has its child group.

    Tuesday, April 2, 2019 10:40 AM
  • User-1174608757 posted

    Hi, athulyashaji

    So, if you just post me about d11 or dt4, it is hard for me to know what is it and what construct is it and what result you want to get. So  it seems that your group depends on the dat a of database table. So, I hope you could show me the construct of your table tbl_chart_item and table and tbl_chart_ledger etc.Else which result do you want to achieve? Could you please show me an img or example?

    Best Regards

    Wei

    Wednesday, April 3, 2019 5:39 AM
  • User-1027332312 posted

    https://imgur.com/a/GNl9Z0y

    the above link have the tables i am using to select the items .

    and i am expecting an output like

    1.Fixed Asset

                 * Plant and Machinery

                                 -(child group or ledger if any)

                 * Building

    2.Current Assets

           *Accounts Receivable

                  -Customer 1

           *Cash and Bank

                   -asset new

    _____________________________________________________________________

    Main Group

            Sub Group

                               Ledger or Child Group

    _______________________________________________________________________________

    Wednesday, April 3, 2019 8:01 AM
  • User-893317190 posted

    Hi athulyashaji ,

    You could customize a class which could store data from you table.The node class has a property named ChildNodes which stores members of child group.

    Below is my code.

     
    public class Node {
    public int Level { get; set; }
    public string NodeName { get; set; }
    public List<Node> ChildNodes { get; set; }
    }
    public partial class mutileveldata : System.Web.UI.Page { public Node RootNode = new Node { Level = 1, NodeName = "assets" ,ChildNodes=new List<Node>()}; private static string constr = ConfigurationManager.ConnectionStrings["EntityDb"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand com = new SqlCommand("select name from table1 where account_name = @name", con)) { con.Open(); com.Parameters.AddWithValue("name", "assets"); using (SqlDataReader reader =com.ExecuteReader()) { while (reader.Read()) { // get child node of assets Node mainGroupNode = new Node { Level = 2, NodeName = reader["name"].ToString(),ChildNodes= new List<Node>()}; // add child node to root node RootNode.ChildNodes.Add(mainGroupNode); // start to add the next level using (SqlConnection con1 = new SqlConnection(constr)) { using (SqlCommand com1 = new SqlCommand("select name from maingroup where sub_group = @name", con1)) { con1.Open(); com1.Parameters.AddWithValue("name", mainGroupNode.NodeName); using (SqlDataReader reader1 = com1.ExecuteReader()) { while (reader1.Read()) { // get child node of maingroup Node subgroupnode = new Node { Level = 3, NodeName = reader1["name"].ToString(), ChildNodes = new List<Node>() }; // add subgroup to maingroup mainGroupNode.ChildNodes.Add(subgroupnode); // start to add next level using (SqlConnection con2 = new SqlConnection(constr)) { using (SqlCommand com2 = new SqlCommand("select name from subgroup where sub_item = @name", con2)) { con2.Open(); com2.Parameters.AddWithValue("name", subgroupnode.NodeName); using (SqlDataReader reader2 = com2.ExecuteReader()) { while (reader2.Read()) { // get the child node of subgroup Node sub_item = new Node { Level = 4, NodeName = reader2["name"].ToString() }; // add child node to subgroup subgroupnode.ChildNodes.Add(sub_item); } } } } } } } } } } } } } } }

    After executing the code above, the RootNode will have all the data divided with group.

    Then you could render it in aspx.

     <form id="form1" runat="server">
            <%-- first level --%>
            <% foreach (var item in RootNode.ChildNodes)
                {
                  
                    
                  %>
           
            <div style="padding-left:0px;color:red "><%=item.NodeName %>  </div>
           
                <%
    
                        foreach (var item1 in item.ChildNodes)
                        {%>
             <%-- second level --%>
                               <div style="padding-left:20px;color:blue "><%=item1.NodeName %>  </div>
                        
                                 <%foreach (var item2 in item1.ChildNodes)
                                     {%>
             <%-- third level --%>
                   <div style="padding-left:40px;color:orange "><%=item2.NodeName %>  </div>
                                     <%} %>
    
                        <%}
    
    
    
    
    
                    } %>
        </form>

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 4, 2019 4:02 AM
  • User-1027332312 posted

    Hi Ackerly Xu,

    It worked.

    Thank you so much.

    Thursday, April 4, 2019 9:05 AM