I have a 'Page' table with parent and child relations like..
PageId Name ParentPageId
1 A 0
2 B 1
3 C 1
4 D 2
5 E 2
6 F 4
My requirement is, I want to copy of the above data based on the pageId.
For example if I pass PageId=1, all the relate rows should copy..
My desired output is like this..
PageId Name ParentPageId
7 A 0
8 B 7
9 C 7
10 D 7
11 E 7
12 F 10
--------------------------------------------------------
For this I used recursion method..
my code is given below..
private void CreatePageCopy(int pId)
{
try
{
objpagebo.Action = "COPY";
objpagebo.PageID = pId;
if (dsChildPages != null)
{
if (dsChildPages.Tables[0].Rows.Count != 0)
{
objpagebo.PageName = dsChildPages.Tables[0].Rows[0]["PageName"].ToString();
}
}
else
{
objpagebo.PageName = HfPageName.Value;
}
objpagebo.ActiveStatus = "Active";
objpagebo.CreationDate = Convert.ToDateTime(System.DateTime.Now).ToString("dd/mm/yyyy");
objpagebo.CreatedBy = Convert.ToString(oEmployeeBO.EmployeeId);
objpagebo.ModifiedBy = Convert.ToString(oEmployeeBO.EmployeeId);
objpagebo.ModificationDate = Convert.ToDateTime(System.DateTime.Now).ToString("dd/MM/yyyy");
DataTable dtGetParentPageId = PageMasterBLL.GetParentPageid(objpagebo);
if (dsChildPages != null)
{
if (dsChildPages.Tables[0].Rows.Count != 0)
{
objpagebo.ParentPageId = Convert.ToInt32(dsChildPages.Tables[1].Rows[0]["ParentId"]);
}
}
else
{
objpagebo.ParentPageId = Convert.ToInt32(dtGetParentPageId.Rows[0]["ParentPageId"]);
}
objpagebo.PageDescription = "";
dsChildPages = PageMasterBLL.InsertPageCopy(objpagebo);
int PageIdForCopy = Convert.ToInt32(dsChildPages.Tables[1].Rows[0]["ParentId"]);
if (dsChildPages.Tables[0].Rows.Count > 0 && dsChildPages != null)
{
foreach (DataRow drChildPages in dsChildPages.Tables[0].Rows)
{
CreatePageCopy(Convert.ToInt32(drChildPages["PageId"]));
}
}
}
catch (Exception Ex)
{
throw Ex;
}
}
But this method will work only if a page have child pages with one level...next level ParentPageId is not comming correctely..because here I am taking Parent PageId as last Inserted row,.
Pls give me a solution..