Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Server Error in '/dynamictreeview' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'node' does not exist in the current context

Source Error:

Line 88: newnode.PopulateOnDemand = true;
Line 89: 
Line 90: node.ChildNodes.Add(newnode);
Line 91: 
Line 92: }



C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {

    }
    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.ChildNodes.Count == 0)

{

switch (e.Node.Depth)

{

case 0:

fillcategory(e.Node);

break;

case 1:

fillsubcat(e.Node);

break;

}

}

}

    private void fillcategory(TreeNode treeNode)
    {
        SqlConnection con = new SqlConnection("Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");

con.Open();

string qry = "select * from tbl_category";

SqlDataAdapter ad = new SqlDataAdapter(qry, con);

DataSet ds = new DataSet();

try

{

ad.Fill(ds);

}

catch (Exception ex)

{

}

if (ds.Tables[0].Rows.Count > 0)

{

foreach (DataRow r in ds.Tables[0].Rows)

{

TreeNode newnode = new TreeNode(r["catname"].ToString() , r["catid"].ToString());

newnode.PopulateOnDemand = true;

node.ChildNodes.Add(newnode);

}



}

    }

    


    private void fillsubcat(TreeNode treeNode)
    {
        string aid = node.Value;

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);

con.Open();

string qry = "select subcatid,subcatname from tbl_subcategory where catid='" + aid + "'";

SqlDataAdapter ad = new SqlDataAdapter(qry, con);

DataSet ds = new DataSet();

try

{

ad.Fill(ds);

}

catch (Exception ex)

{

}

if (ds.Tables[0].Rows.Count > 0)

{

foreach (DataRow r in ds.Tables[0].Rows)

{

TreeNode newnode = new TreeNode(r["subcatname"].ToString(), r["subcatid"].ToString());

newnode.PopulateOnDemand = false;

newnode.SelectAction = TreeNodeSelectAction.None;

node.ChildNodes.Add(newnode);

}
}
}
}
Posted
Updated 12-Feb-12 19:07pm
v2

What can be unclear in this self-explanatory error message? How can you use the node which you never declared?

—SA
 
Share this answer
 
Comments
manognya kota 13-Feb-12 1:51am    
+5 :)
Sergey Alexandrovich Kryukov 13-Feb-12 2:05am    
Thank you, Manognya.
--SA
Hi,

As per my understanding you are trying to add the node to your treeview. But that is the code is where the error is caused.If you see the error ,

The name 'node' does not exist in the current context


There is no variable with name as 'node'.Also, it doesnt specify where are you adding the new node.I guess that newnode should be added to the node passed as parameter.

Try

treeNode.ChildNodes.Add(newnode);


Please see the below link on adding nodes to your treeview.

http://stackoverflow.com/questions/881607/adding-child-nodes-in-treeview[^]

http://msdn.microsoft.com/en-us/library/aa984278(v=vs.71).aspx[^]

Hope this helps.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 13-Feb-12 1:48am    
Yes, and what did I say? :-)
My 5.
--SA
manognya kota 13-Feb-12 1:51am    
Same thing:).I was typing the solution and saw your solution after i posted.
Thanks:)
I would suggest you to Clean the solutions. remove all your breakpoints and build it again.
And then try to compile it
 
Share this answer
 
Comments
rockpune 13-Feb-12 1:17am    
ok sir
rockpune 13-Feb-12 1:20am    
i tryed but not getting output sir
Sergey Alexandrovich Kryukov 13-Feb-12 1:47am    
Yes, because this is not a reason. This is a bug I explain below.
You got too correct answers, please see.
--SA
RDBurmon 13-Feb-12 1:37am    
Did you cleaned both (solutions / all breakpoints) ?
Sergey Alexandrovich Kryukov 13-Feb-12 1:46am    
This is irrelevant. This is nothing but a bug. The name "node" is really used and really never declared. Just search the code to see it.
Sorry, I just have to vote 1 in this case.
--SA

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