locked
CS1503: Argument 1: cannot convert from 'string' to 'System.Web.UI.WebControls.TreeNode' //what should I do RRS feed

  • Question

  • User-587451504 posted

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;

    namespace budgetfixingsystem
    {
    public partial class AccountTree : System.Web.UI.Page
    {
    SqlConnection cn = new SqlConnection("server=DESKTOP-1KI8UBE;database=BudgetAccount;Trusted_Connection=True");
    protected void Page_Load(object sender, EventArgs e)
    {
    //cn.Open();
    //Response.Write(cn.State.ToString());
    //cn.Close();


    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter("Select * from BudgetAccounts", cn);
    da.Fill(dt);
    TreeView1.Nodes.Add("BudgetAccounts");
    foreach (DataRow dr in dt.Rows)
    {
    TreeNode nod = new TreeNode(dr["Accname"].ToString());
    nod.ChildNodes.Add(dr["AccID"].ToString());
    nod.ChildNodes.Add(dr["ParAcc"].ToString());
    TreeView1.Nodes.Add(nod);
    }


    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter("Select * from BudgetAccounts Where Accname like '%" + TextBox1.Text + "% '", cn);

    da.Fill(dt);
    TreeView1.Nodes.Clear();
    TreeView1.Nodes.Add("BudgetAccounts");
    foreach (DataRow dr in dt.Rows)
    {
    TreeNode nod = new TreeNode(dr["Accname"].ToString());
    nod.ChildNodes.Add(dr["AccID"].ToString());
    nod.ChildNodes.Add(dr["ParAcc"].ToString());
    TreeView1.Nodes.Add(nod);
    }
    }
    }
    }

    Wednesday, March 13, 2019 11:14 AM

All replies

  • User-1038772411 posted

    Initialize your groupNode variable when you first declare it (null or an empty node, depending on your requirements) liker

    Declare this before use:

    TreeNode nod =new TreeNode();

    Wednesday, March 13, 2019 11:30 AM
  • User-893317190 posted

    Hi  safaruda,

    As AddWeb Solution Pvt. Ltd  has said , you should  initialize the new TreeNode  and add to the treeView.

    After that , you could add all the subnode to the new node instead of TreeView if you want to use BudgetAccounts as the rootNode of your TreeView.

    Below is my code.

      protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add(new DataColumn("Accname"));
                dataTable.Columns.Add(new DataColumn("Accid"));
                dataTable.Columns.Add(new DataColumn("ParAcc"));
                dataTable.Rows.Add("namecy", "1234", "Minus");
                dataTable.Rows.Add("kerry", "4567", "info");
                TreeNode rootNode = new TreeNode("BudgetAccounts");
                TreeView1.Nodes.Add(rootNode);
    
                foreach (DataRow item in dataTable.Rows)
                {
                    TreeNode nod = new TreeNode(item["Accname"].ToString());
                    nod.ChildNodes.Add(new TreeNode(item["Accid"].ToString()));
                    nod.ChildNodes.Add(new TreeNode(item["ParAcc"].ToString()));
                    rootNode.ChildNodes.Add(nod);
    
                }
       
                rootNode.CollapseAll();
            }

    The result.

    Best regards,

    Ackerly Xu

    Thursday, March 14, 2019 5:29 AM