In the below function I have a DataTable which resets during recursion of the function. How can I solve this problem?
I have to create table once and add rows repeatedly in each repeat of the function.
Please help!
private DataTable GetSelectedTreeNodes(TreeNode parentTreeNode)
{
DataTable tvp = new DataTable();
tvp.Columns.Add(new DataColumn("Suburb"));
if (parentTreeNode.Checked)
{
ListBox1.Items.Add(parentTreeNode.Text);
tvp.Rows.Add(parentTreeNode.Text);
tvp.Rows.Add(parentTreeNode.Text);
}
if (parentTreeNode.ChildNodes.Count > 0)
{
foreach (TreeNode childTreeNode in parentTreeNode.ChildNodes)
{
GetSelectedTreeNodes(childTreeNode);
}
}
return tvp;
}

Syed ShanuRank #87 · 23.7K reputationPosted Apr 23, 2015, 12:29 AM EDT
If is this is Web application then
DataTable tvp;
publiv void Page_Load
{
if(!page.ispostback)
{
tvp = new DataTable();
tvp.Columns.Add(new DataColumn("Suburb"));
}
}
//here your method
Prakash Malakar10 reputationPosted Apr 23, 2015, 1:49 AM EDT
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
CSS
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Line 295: tvp.Rows.Add(parentTreeNode.Text); Line 296: tvp.Rows.Add(parentTreeNode.Text); Line 297: