0% found this document useful (0 votes)
63 views4 pages

Tree View

The document describes code for populating a treeview control with hierarchical data from a database. It retrieves data from a stored procedure, maps it to objects, and adds it to the treeview by finding parent nodes.

Uploaded by

prasid_joshi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views4 pages

Tree View

The document describes code for populating a treeview control with hierarchical data from a database. It retrieves data from a stored procedure, maps it to objects, and adds it to the treeview by finding parent nodes.

Uploaded by

prasid_joshi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

using using using using using using using using using using using using

System; System.Data; System.Configuration; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls; System.IO; System.Collections.Generic; System.Data.SqlClient;

public partial class Case : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { PopulateTreeview(); } private void PopulateTreeview() { this.tvHierarchyView.Nodes.Clear(); HierarchyTrees hierarchyTrees = new HierarchyTrees(); HierarchyTrees.HTree objHTree=null; using (SqlConnection connection = new SqlConnection(@"server = 127.0.0.1 ; Integrated security = true; Initial Catalog = CRM")) { connection.Open(); using (SqlCommand command = new SqlCommand("GET_HIERARCHY", connecti on)) { command.CommandType = System.Data.CommandType.StoredProcedure; SqlDataReader reader = command.ExecuteReader(System.Data.Command Behavior.CloseConnection); while (reader.Read()) { objHTree=new HierarchyTrees.HTree(); objHTree.LevelDepth = int.Parse(reader["LEVEL_DEPTH"].ToStri ng()); objHTree.NodeID = int.Parse(reader["PROBLEM_ID"].ToString()) ; objHTree.UnderParent = int.Parse(reader["UNDER_PARENT"].ToSt ring()); objHTree.NodeDescription = reader["PROBLEM_DESCRIPTION"].ToS tring(); hierarchyTrees.Add(objHTree); } } } foreach (HierarchyTrees.HTree hTree in hierarchyTrees) { HierarchyTrees.HTree parentNode = hierarchyTrees.Find(delegate(Hiera rchyTrees.HTree emp) { return emp.NodeID == hTree.UnderParent; }); if (parentNode != null) { foreach (TreeNode tn in tvHierarchyView.Nodes)

{ if (tn.Value == parentNode.NodeID.ToString()) { tn.ChildNodes.Add(new TreeNode(hTree.NodeDescription.ToS tring(), hTree.NodeID.ToString())); } if (tn.ChildNodes.Count > 0) { foreach (TreeNode ctn in tn.ChildNodes) { RecursiveChild(ctn, parentNode.NodeID.ToString(), hT ree); } } } } else { tvHierarchyView.Nodes.Add(new TreeNode(hTree.NodeDescription, hT ree.NodeID.ToString())); } } tvHierarchyView.ExpandAll(); } public void RecursiveChild(TreeNode tn, string searchValue, HierarchyTrees.H Tree hTree) { if (tn.Value == searchValue) { tn.ChildNodes.Add(new TreeNode(hTree.NodeDescription.ToString(), hTr ee.NodeID.ToString())); } if (tn.ChildNodes.Count > 0) { foreach (TreeNode ctn in tn.ChildNodes) { RecursiveChild(ctn, searchValue,hTree); } } } public class HierarchyTrees : List<HierarchyTrees.HTree> { public class HTree { private string m_NodeDescription; private int m_UnderParent; private int m_LevelDepth; private int m_NodeID; public int NodeID { get {return m_NodeID;} set { m_NodeID=value; } } public string NodeDescription {

get { return m_NodeDescription; } set { m_NodeDescription = value; } } public int UnderParent { get { return m_UnderParent; } set { m_UnderParent = value; } } public int LevelDepth { get { return m_LevelDepth; } set { m_LevelDepth = value; } } } } protected void saveProblem_Click(object sender, EventArgs e) { } public void insertInto(string SQL) { SqlConnection sqlCon; string StrCon = "server = 127.0.0.1; Integrated security = true; Initial Catalog = CRM"; sqlCon = new SqlConnection(StrCon); sqlCon.Open(); try { SqlCommand sqlCmd = new SqlCommand(); // string customerCode = TextBox2.Text.Trim().Replace("'", "''"); // string priority = DropDownList1.SelectedValue; // string problemName = TextBox1.Text.Trim().Replace("'", "''"); sqlCmd.CommandText = SQL; sqlCmd.Connection = sqlCon; sqlCmd.ExecuteNonQuery(); sqlCon.Close(); } catch (Exception ex) { string Err = ex.ToString(); } } public int getID(string SQL) { SqlConnection sqlCon; int parentID = 0; string StrCon = "server = 127.0.0.1; Integrated security = true; Initial Catalog = CRM"; sqlCon = new SqlConnection(StrCon); sqlCon.Open(); try { SqlCommand sqlCmds = new SqlCommand(); sqlCmds = new SqlCommand(SQL, sqlCon); SqlDataReader Sdrs = sqlCmds.ExecuteReader(); while (Sdrs.Read()) {

parentID = int.Parse((string)Sdrs["UNDER_PARENT"].ToString()); } } catch (Exception ex) { string Err = ex.ToString(); } return parentID; } protected void addNewCategory_Click(object sender, EventArgs e) { MultiView1.ActiveViewIndex = 0; } protected void edit_Click(object sender, EventArgs e) { MultiView1.ActiveViewIndex = 1; } protected void subProblem_Click(object sender, EventArgs e) { MultiView1.ActiveViewIndex = 2; } protected void deleteLeaf_Click(object sender, EventArgs e) { MultiView1.ActiveViewIndex = 3; } protected void saveRoot_Click(object sender, EventArgs e) { string sql = " Insert Into Problem Values('" + problemName.Text + "','0' ,'" + priority.Text + "')"; insertInto(sql); Response.Redirect("Case.aspx"); }

You might also like