user1912987
user1912987

Reputation: 115

Regarding Tree Control in c#

This is my code for retrieving data from database and displaying it in Tree control

String empid = ValueBox1.Text;
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["EmployeeDatabase"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("ReportingManagers", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@EmpID",SqlDbType.Int,0).Value =  empid;

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
                //Here "Node" Means It Will Add Nodes As All Root Nodes...
                TreeView1.Nodes.Add(dr.GetValue(0).ToString());
 }
 dr.Close();

I am getting error in this line:

 TreeView1.Nodes.Add(dr.GetValue(0).ToString());

Error :The best overloaded method match for 'System.Web.UI.WebControls.TreeNodeCollection.Add(System.Web.UI.WebControls.TreeNode)' has some invalid arguments.

Error : Argument 1: cannot convert from 'string' to 'System.Web.UI.WebControls.TreeNode'

Please help me to solve this issue..

Upvotes: 0

Views: 454

Answers (2)

Beth Lang
Beth Lang

Reputation: 1905

You need to create a new instance of the TreeNode class instead of trying to add the String value directly.

See this link for an example http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.nodes.aspx

Upvotes: 0

Ravi Y
Ravi Y

Reputation: 4376

The error message you got is pretty self explanatory. In the below line, it expects a TreeNode object to be added to the collection.

TreeView1.Nodes.Add(dr.GetValue(0).ToString());

Try this:

TreeView1.Nodes.Add(new TreeNode(dr.GetValue(0).ToString()));

Upvotes: 2

Related Questions