Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have this sql statement......
SQL
create table categories
( id       integer     not null  primary key
, name     varchar(37) not null
, parentid integer     null
, foreign key parentid_fk (parentid)
      references categories (id)
);

And i wanna display its Parent first and by clicking parent show its sub category
using datalist.. I've data like
id	name	parentid
1	animal	NULL
2	vegetable	NULL
3	mineral	NULL
4	doggie	1
5	kittie	1
6	horsie	1
7	gerbil	1
8	birdie	1
9	carrot	2
10	tomato	2
11	potato	2
12	celery	2
13	rutabaga	2
14	quartz	3
15	feldspar	3
16	silica	3
17	gypsum	3
18	hunting	4
19	companion	4
20	herding	4
21	setter	18
22	pointer	18
23	terrier	18
24	poodle	19
25	chihuahua	19
26	shepherd	20
27	collie	20

Is anyone help me...
Thank you in advance
Posted
Updated 14-Jun-12 20:59pm
v2
Comments
m@dhu 26-Apr-11 6:58am    
And i wanna display its Parent first and by clicking parent
where you want to display the parent and by clicking what? Did you try anything? Explain bit more clear.
Sandeep Mewara 26-Apr-11 7:31am    
Not clear. Please re-phrase. Edit the question using 'Improve Question'

1 solution

Hi,

You can do it in two sql statements.

The below query will get all your parent categories.
Select id, name from categories where parentid is NULL


Then when the user selects a category you know its Id, so then you just select all the child categories which have a parent category matching the selected category.
//Pass in parameter @ParentId which matches the Category clicked.
Select id, name from categories where parentid = @ParentId
 
Share this answer
 
Comments
Member 7777423 27-Apr-11 7:12am    
thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900