|
|
ASP.Net Treeview Filling using SQL SERVER Recursion I was desperately searching for a single Sql recursive query and i got an idea to accomplish it using CTE(Common Table expressions - SqlServer 2005 feature).It can return the hierarchical dataset of a particular parent/parents with depth/Level . In my project recursion is needed in many scenarios, like getting the complete set of reference ids from a self reference table, for filling a treeview . To avoid recursion in .Net code I tried in a different way and the outcome is like this; This is an example of an organization data which contains employees and their reporting officers in the same self referenced table. Table structure
With a normal query, hierarchical data fetching is not possible if we are specifying a particular parent id. Sample Sql Query is given below for fetching data hierarchically with hierarchical depth level. We can use this for fetching the records of a particular parent also. SQL Query WITH SimpleRecursive AS(SELECT Employee_Nm,Employee_Id,Parent,0 AS depth FROM Recursion WHERE (Employee_Id IN(SELECT Employee_Id FROM Recursion AS Recursion_1 WHERE (Parent=1)))
UNION ALL Sql query pane with result set :
Then i was trying to populate this result set in to a tree view. I could find this is done using Recursive methods for adding the child nodes. I thought of doing it without using recursive methods and finally i got solution for that. For that first we need to fill the dataset with this query result. VB.Net code window: - Treeview
'//This procedure accepts dataset and an optional parentid as parameters.
Try For Each TvwNode In CollItems Catch ex As Exception
As per my observation this method is relatively fast and easy to implement (with few lines of code).
|




