New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Saving changes made in RadTreeView to a database manually

How to

Save the changes made in RadTreeView to a database.

Solution

The example demonstrates a simple mechanism to attain such functionality using Access database with single self-referencing table containing columns id,parentId and Text. The structure of the treeview is loaded from this database in the PageLoad. Then the user edits the text of random nodes in the treeview and hits the Save button on the page. Thus a postback is triggered and the updated information is saved back in the database file.
Please review the code below for your better understanding:

ASPX
    private void GenerateTreeView() 
    { 
        OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/Tree.mdb")); 
        OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Links", dbCon); 
        DataSet ds = new DataSet(); 
        adapter.Fill(ds); 
        ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["id"], ds.Tables[0].Columns["parentId"]); 
        foreach (DataRow dbRow in ds.Tables[0].Rows) 
        { 
            if (dbRow.IsNull("parentId")) 
            { 
                RadTreeNode node = CreateNode(dbRow["Text"].ToString(), true); 
                RadTreeView1.Nodes.Add(node); 
                RecursivelyPopulate(dbRow, node); 
            } 
        } 
    } 
    private void RecursivelyPopulate(DataRow dbRow, RadTreeNode node) 
    { 
        foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation")) 
        { 
            RadTreeNode childNode = CreateNode(childRow["Text"].ToString(), true); 
            node.Nodes.Add(childNode); 
            RecursivelyPopulate(childRow, childNode); 
        } 
    } 
    private RadTreeNode CreateNode(string text, bool expanded) 
    { 
        RadTreeNode node = new RadTreeNode(text); 
        node.Expanded = true; 
        return node; 
    } 
    private void Page_Load(object sender, System.EventArgs e) 
    { 
        if (!Page.IsPostBack) 
        { 
            GenerateTreeView(); 
        } 
    } 
    protected void RadTreeView1_NodeEdit(object o, RadTreeNodeEditEventArgs e) 
    { 
        RadTreeNode edit = e.Node; 
        edit.Text = e.Text; 
    } 
    protected void Button1_Click(object sender, System.EventArgs e) 
    { 
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/Tree.mdb")); 
        OleDbCommand command = new OleDbCommand("DELETE * from Links", con); 
        con.Open(); 
        command.ExecuteNonQuery(); 
        con.Close(); 
        UpdateNodes(); 
    } 
    private void UpdateNodes() 
    { 
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/Tree.mdb")); 
        con.Open(); 
        IList<RadTreeNode> allNodes = RadTreeView1.GetAllNodes(); 
        for (int i = 0; i < allNodes.Count; i++) 
        { 
            RadTreeNode node = (RadTreeNode) allNodes[i]; 
            int parentId = allNodes.IndexOf(node.ParentNode); 
            OleDbCommand command = new OleDbCommand("INSERT into Links([id], parentId,[Text]) values (@ID,@parentId,@Text)", con); 
            command.Parameters.AddWithValue("ID", i.ToString()); 
            if (parentId > -1) 
            { 
                command.Parameters.AddWithValue("parentId", parentId); 
            } 
            else 
            { 
                command.Parameters.AddWithValue("parentId", System.DBNull.Value); 
            } 
            command.Parameters.AddWithValue("Text", node.Text); 
            command.ExecuteNonQuery(); 
        } 
        con.Close(); 
    } 
In this article
How toSolution
Not finding the help you need?
Contact Support