The last suggestion should work, but I would prefer to normalize the solution further to organize the data better
and to be more scalable in case more options arise, and there are several ways to do this.
One is to have a single table named Categories that has all names and IDs like below:
ID CategoryName
1 Color
2 Size
3 Medium
4 Price
5 Discount
The the second table would be a lookup of the 1st table. If you are certain
there would only be 2 levels ever you can identify the ID type exlicitly with a bit field. If you think it ever could grow, you could further normalize and add a 3rd table that is called Category Type. 1st the table with the explicity definition
ID fkCategoryName IsParentCategory fkParentCategoryID
1 2 1 NULL
2 5 0 4
...or you could make a 3rd table that could contain 1..n Category types like below:
ID CategoryType
1 Parent
2 SubCategory
..and then change the above table to the following:
ID fkCategoryName fkCategoryType fkParentCategoryID
1 2 1 NULL
2 5 2 4
Then to implement this physically in the ASP.NET application you could use a control like a GridView to allow add/edit/delete capability of the different variations above. If your criteria changes and you can have 1...n subcategories at runtime, you might
want to look into a TreeView control where child items could be dynamically added.
Member
217 Points
389 Posts
parent and child items
Aug 26, 2010 07:21 AM|gridview|LINK
I have to create a aspx page where end users can create, edit and delete category and their subCategory from it.
Please suggest what is simple way to do above? using .Net
PS: a category can have zero or more subcategory and subcategory will not have any of its own subcategory, i.e. there are only two levels.
Contributor
7260 Points
1902 Posts
Re: parent and child items
Aug 26, 2010 07:58 AM|nareshguree23@gmail.com|LINK
create a table
like
Id CateName ParentId
1 Electronic 0
2 Freeze 1
3 Mobile 1
4 Jewellery 0
5 Ring 4
6 Ear Ring 4
the above table represent you both cate and sub cate
if parent id= 0 means Main Category
if parent id > 0 it means this is sub category of parent id.
and you can add/edit/detele the same record in single table.
Star
12060 Points
2740 Posts
Re: parent and child items
Aug 26, 2010 11:32 AM|atconway|LINK
The last suggestion should work, but I would prefer to normalize the solution further to organize the data better and to be more scalable in case more options arise, and there are several ways to do this.
One is to have a single table named Categories that has all names and IDs like below:
ID CategoryName
1 Color
2 Size
3 Medium
4 Price
5 Discount
The the second table would be a lookup of the 1st table. If you are certain there would only be 2 levels ever you can identify the ID type exlicitly with a bit field. If you think it ever could grow, you could further normalize and add a 3rd table that is called Category Type. 1st the table with the explicity definition
ID fkCategoryName IsParentCategory fkParentCategoryID
1 2 1 NULL
2 5 0 4
...or you could make a 3rd table that could contain 1..n Category types like below:
ID CategoryType
1 Parent
2 SubCategory
..and then change the above table to the following:
ID fkCategoryName fkCategoryType fkParentCategoryID
1 2 1 NULL
2 5 2 4
Then to implement this physically in the ASP.NET application you could use a control like a GridView to allow add/edit/delete capability of the different variations above. If your criteria changes and you can have 1...n subcategories at runtime, you might want to look into a TreeView control where child items could be dynamically added.