Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

i get this error while trying to populate the treeview from sql database

the error
Object reference not set to an instance of an object

the code

C#
private void PopulateTreeview()
    {

        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand com = new SqlCommand("GLD_CHART_OF_ACCOUNTS_some_select", con);
        com.CommandType = CommandType.StoredProcedure;

        con.Open();
        SqlDataAdapter adbt = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        com.ExecuteNonQuery();
        adbt.Fill(ds, "GLD_CHART_OF_ACCOUNTS");
        con.Close();
        DataTable dt = ds.Tables[0];
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string nodeID = ds.Tables[0].Rows[i][0].ToString(); //Get the node code 'Node number'
            string nodeName = ds.Tables[0].Rows[i][1].ToString(); // Get the node name
            string Fullname = nodeID + nodeName; //Concatenate the number + name to get the full node name
            string parent_ID = ds.Tables[0].Rows[i][2].ToString(); //Get the parent ID of the node

            if (parent_ID == "0")
            {
                TreeNode node = new TreeNode();
                node.Text = Fullname;
                TreeView1.Nodes.Add(node);
            }
            else
            {
                //GLD_CHART_OF_ACCOUNTSSelect
                SqlCommand command = new SqlCommand("GLD_CHART_OF_ACCOUNTSSelect", con);
                command.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adp = new SqlDataAdapter(command);
                DataSet IdSet = new DataSet();
                {
                    con.Open();
                    command.Parameters.Add("@ACCOUNT_CODE", DbType.Int32).Value = Convert.ToInt32(parent_ID);
                }
                command.ExecuteNonQuery();
                adp.Fill(IdSet, "GLD_CHART_OF_ACCOUNTS");
                con.Close();
                string Parent_name = IdSet.Tables[0].Rows[0][0].ToString() + IdSet.Tables[0].Rows[0][1].ToString();
                TreeNode node = new TreeNode();
                node.Text = Fullname;
                TreeView1.FindNode(Parent_name).ChildNodes.Add(node);
            }
        }

    }


when i try to add child node to child
like

node A
sub A
Sub Of sub A
return this error
any help
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900