0
Answer

TreeView control Error

Sridhar Y

Sridhar Y

15y
4.4k
1

public string strConn = "Persist Security Info=False;Integrated Security=true;Initial Catalog=SampleDB;server=(local)";
        protected void Page_Load(object sender, EventArgs e)
        {
            //if(!IsPostBack)
            //clearControls();
            fillManager();
            btnSave.Attributes.Add("onclick()", "return validateFun()");
            btnTree.Attributes.Add("onclick()", "return showtree()");
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string chkmgr = "N";
            if(chkIsMgr.Checked == true)
                chkmgr = "Y";
            else
                chkmgr = "N";
           
            SqlConnection sqlConn = new SqlConnection(strConn);
            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.CommandText = "insert into employeeMgr values('" + txtEmpID.Text.ToString() + "', '" + txtEmpName.Text.ToString() + "','" + chkmgr + "', '" + ddlMgrID.Text + "')";
            sqlCmd.Connection = sqlConn;
            sqlConn.Open();
            sqlCmd.ExecuteNonQuery();
            sqlConn.Close();
            fillManager();
            clearControls();
        }
        private void fillManager()
        {
            SqlConnection sqlConn = new SqlConnection(strConn);
            SqlCommand sqlCmd = new SqlCommand("select empid,empname from employeeMgr", sqlConn);
            SqlDataAdapter adpter = new SqlDataAdapter(sqlCmd);
            DataSet dsmpls = new DataSet();
            adpter.Fill(dsmpls);
            ddlMgrID.DataSource = dsmpls.Tables[0];
            ddlMgrID.DataTextField = "empname";  //dsmpls.Tables[0].Rows[0]["empname"].ToString();
            ddlMgrID.DataValueField = "empid";   //  dsmpls.Tables[0].Rows[0]["empid"].ToString();
            ddlMgrID.DataBind();
        }
        protected void btnTree_Click(object sender, EventArgs e)
        {
          //Response.Redirect( 
        }
        public void clearControls()
        {
            txtEmpID.Text = string.Empty;
            txtEmpName.Text = string.Empty;
            chkIsMgr.Checked = false;
            //ddlMgrID.Text = "--Select--";
        }
-----
<asp:TreeView ID="treeEmployee" runat="server" OnTreeNodePopulate="Node_Populate">
        <Nodes>
            <asp:TreeNode Text="Employees" PopulateOnDemand="true" Value="0">
            </asp:TreeNode>
        </Nodes>
    </asp:TreeView>
-----
public void Node_Populate(object sender, System.Web.UI.WebControls.TreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count == 0)
            {
                switch (e.Node.Depth)
                {
                    case 0:
                        FillManager(e.Node);
                        break;
                    case 1:
                        FillEmployee(e.Node);
                        break;
                }//End Switch
            } //if
        } //node_polulate()
        public void FillManager(TreeNode Node)
        {
            string strConn = "Persist Security Info=False;Integrated Security=true;Initial Catalog=SampleDB;server=(local)";
            SqlConnection connSql = new SqlConnection(strConn);
            SqlCommand comnd = new SqlCommand("select distinct mgrid from employeeMgr where ismanamger = 'Y'", connSql);
            SqlDataAdapter adpter = new SqlDataAdapter(comnd);
            DataSet dsdept = new DataSet();
            adpter.Fill(dsdept);
            //TreeView1.DataSource = dsdept;
            //TreeView1.DataBind();
            if (dsdept.Tables.Count > 0)
            {
                foreach (DataRow row in dsdept.Tables[0].Rows)
                {
                    TreeNode newNode = new TreeNode(row["mgrid"].ToString());
                    newNode.PopulateOnDemand = true;
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                    Node.ChildNodes.Add(newNode);
                }
            }

        } //FillManager()
        public void FillEmployee(TreeNode Node)
        {
            string strmgrid = Node.Value;
            string strConn = "Persist Security Info=False;Integrated Security=true;Initial Catalog=SampleDB;server=(local)";
            SqlConnection connSql = new SqlConnection(strConn);
            SqlCommand comnd = new SqlCommand("select distinct empname from employeeMgr where mgrid = '" + strmgrid + "'", connSql);
            SqlDataAdapter adpter = new SqlDataAdapter(comnd);
            DataSet dsemp = new DataSet();
            adpter.Fill(dsemp);
            if (dsemp.Tables.Count > 0)
            {
                foreach (DataRow row in dsemp.Tables[0].Rows)
                {
                    TreeNode newNode = new TreeNode(row["empname"].ToString());
                    newNode.PopulateOnDemand = true;
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                    Node.ChildNodes.Add(newNode);
                }
            }
        } //FillEmployee()