2014年7月13日 星期日

[RESOLVED] Binding data from table to Treeview in ASP.NET C#


Hi friends, I am trying to bind table values to treeview control in asp.net c#. I want the output of treeview as following:


 


Control1


       Sub-Control1


       Sub-Control2


       Sub-Control3


Control2


       Sub-Control1


       Sub-Control2


       Sub-Control3


Control3


       Sub-Control1


       Sub-Control2


       Sub-Control3


Control4


       Sub-Control1


       Sub-Control2


       Sub-Control3


 


But I’m getting “Object Reference not set to an instance of object” Error. Please help me. I have posted my code below.


 


Thanks.


 


SqlCommand cmd_Select_Parent_Nodes = new SqlCommand("Select DISTINCT control_Type from tbl_Controls", con);


                SqlDataAdapter da_Parent = new SqlDataAdapter(cmd_Select_Parent_Nodes);


                ds_Parent = new DataSet();


                da_Parent.Fill(ds_Parent);


 


 


                int parentNodes = ds_Parent.Tables[0].Rows.Count;


 


                TreeNode[] HeaderNode = new TreeNode[parentNodes];


 


                for (int i = 0; i < parentNodes; i++)


                {


 


                    string header = ds_Parent.Tables[0].Rows[i]["control_Type"].ToString();


 


                    HeaderNode[i].Text = header;


 


 


                    SqlCommand cmd_Select_Child_Nodes = new SqlCommand("Select control_Name from tbl_Controls where control_Type = @Type", con);


                    cmd_Select_Child_Nodes.Parameters.AddWithValue("@Type", HeaderNode[i].Text);


                    DataSet ds_Child = new DataSet();


                    SqlDataAdapter da_Child = new SqlDataAdapter(cmd_Select_Child_Nodes);


                    da_Child.Fill(ds_Child);


                   


                    int childNodes = ds_Child.Tables[0].Rows.Count;


 


                    TreeNode[] RootNodes = new TreeNode[childNodes];


 


                    for (int node = 0; node < childNodes; node++)


                    {


                        RootNodes[node].Text = ds_Child.Tables[0].Rows[node]["control_Name"].ToString();


                        HeaderNode[i].ChildNodes.Add(RootNodes[node]);


                    }


controls_TreeView.Nodes.Add(HeaderNode[i]);


}




hi,


avoid use  TreeNode[] it is too easy to make mistake .  childnode is relation with parentNode .we can use a loop that contains the loop to load child nodes .


such as :


foreach (DataRow c in dtTree)
{
TreeNode ParentNode = new TreeNode();
ParentNode.Text = c.StudentID;
ParentNode.Value = c.StudentID;
TreeView1.Nodes.Add(ParentNode);
List list2 = dal.FindAll();
foreach (DataRow d in dtTree.Select("control_Type = " + c.id))
{
TreeNode ChildNode = new TreeNode();
ChildNode.Text = d.StudentID;
ChildNode.Value = d.StudentID;
ParentNode.ChildNodes.Add(ChildNode);
}
}



Hope this helps


 


 


 



to find the actual reason of this error, please have a detailed look on the exception object and specially the stack-strace. to me, following lines of code can be
problematic if do not check them for null/empty values, as it is not necessary the every parent must has childs in database.


// if "control_Type" allows null it will fail
string header = ds_Parent.Tables[0].Rows[i]["control_Type"].ToString();

// if parent has zero childs then it may crash
int childNodes = ds_Child.Tables[0].Rows.Count;

i hope you got my point, thanks 




沒有留言:

張貼留言