Create Tables
/****** Object: Table [dbo].[tblReports] ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblReports]') AND type in (N'U'))
DROP TABLE [dbo].[tblReports]
GO
/****** Object: Table [dbo].[tblReportDetails] ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblReportDetails]') AND type in (N'U'))
DROP TABLE [dbo].[tblReportDetails]
/****** Object: Table [dbo].[tblReports] Script Date: 07/02/2011 23:37:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblReports]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblReports](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ReportID] [int] NOT NULL,
[Text] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ReportLink] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Descr] [varchar](150) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ParentID] [int] NULL,
[IsLink] [bit] NULL,
[IsVisible] [bit] NULL,
CONSTRAINT [PK_tblReports] PRIMARY KEY CLUSTERED
(
[ReportID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
SET IDENTITY_INSERT [dbo].[tblReports] ON
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (1, 1, N'Parent 1', NULL, N'Management', NULL, 0, 1)
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (2, 2, N'Parent 2', NULL, N'Broking', NULL, 0, 1)
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (3, 3, N'Parent 3', NULL, N'Account', NULL, 0, 1)
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (4, 4, N'Child 3.1', N'child31.aspx', N'Child 3.1', 3, 1, 1)
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (5, 5, N'Child 2.1', N'Child21.aspx', N'Child 2.1', 2, 1, 1)
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (6, 6, N'Child 2.2', N'child22.aspx', N'Child 2.2', 2, 1, 1)
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (7, 7, N'Child 3.2', N'child32.aspx', N'Child 3.2', 3, 1, 1)
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (8, 8, N'Child 1.1', N'child11.aspx', N'Child 1.1', 1, 1, 1)
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (9, 9, N'Child 2.3', N'Null', N'Child 2.3', 2, 0, 1)
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (10, 10, N'Child 2.3.1', N'child231.aspx', N'Child 2.3.1', 9, 1, 1)
INSERT [dbo].[tblReports] ([ID], [ReportID], [Text], [ReportLink], [Descr], [ParentID], [IsLink], [IsVisible]) VALUES (11, 11, N'Child 2.3.2', N'child232.aspx', N'Child 2.3.2', 9, 1, 1)
SET IDENTITY_INSERT [dbo].[tblReports] OFF
/****** Object: Table [dbo].[tblReportDetails ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblReportDetails]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblReportDetails](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RoleID] [int] NOT NULL,
[ReportID] [int] NOT NULL,
CONSTRAINT [PK_tblReportDetails] PRIMARY KEY CLUSTERED
(
[RoleID] ASC,
[ReportID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
SET IDENTITY_INSERT [dbo].[tblReportDetails] ON
INSERT [dbo].[tblReportDetails] ([ID], [RoleID], [ReportID]) VALUES (1, 1, 1)
INSERT [dbo].[tblReportDetails] ([ID], [RoleID], [ReportID]) VALUES (2, 1, 2)
INSERT [dbo].[tblReportDetails] ([ID], [RoleID], [ReportID]) VALUES (3, 1, 3)
INSERT [dbo].[tblReportDetails] ([ID], [RoleID], [ReportID]) VALUES (4, 2, 2)
INSERT [dbo].[tblReportDetails] ([ID], [RoleID], [ReportID]) VALUES (5, 2, 3)
INSERT [dbo].[tblReportDetails] ([ID], [RoleID], [ReportID]) VALUES (6, 3, 3)
SET IDENTITY_INSERT [dbo].[tblReportDetails] OFF
Create aspx page and add a tree view control
Put below code in .cs page and do appropriate changes
protected void Page_Load(object sender, EventArgs e)
{
GetData();
}
void GetData()
{
string qry="Select * from tblReports";
DataTable dt = dal.GetRecords(qry); //Put the code of get the data from Database
if (dt != null && dt.Rows.Count > 0)
{
AddTopTreeNodes(dt);
}
}
void AddTopTreeNodes(DataTable dt)//Bind Parent Nodes Here
{
DataView dvP = dt.DefaultView;
dvP.RowFilter = "ParentID is Null";
foreach (DataRowView row in dvP)
{
TreeNode PNode = new TreeNode(row["Text"].ToString(), row["ReportID"].ToString(), "", row["ReportLink"].ToString(), "_self");
tv1.Nodes.Add(PNode);
AddChildTreeNodes(dt, PNode);
}
}
void AddChildTreeNodes(DataTable dt,TreeNode pNode)//Add Child Nodes Here
{
DataView dvP = dt.DefaultView;
dvP.RowFilter = "ParentID="+pNode.Value;
foreach (DataRowView row in dvP)
{
TreeNode CNode = new TreeNode(row["Text"].ToString(), row["ReportID"].ToString(), "", row["ReportLink"].ToString(), "_self");
pNode.ChildNodes.Add(CNode);
AddChildTreeNodes(dt, CNode);//Add Nested ChildNodes Here
}