Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
I have this sql statement......
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 26 Apr '11 - 0:37
Edited 14 Jun '12 - 20:59
Nikfazan5.5K

Comments
mkgoud - 26 Apr '11 - 6:58
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:31
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
  Permalink  
Comments
Member 7777423 - 27 Apr '11 - 7:12
thanks.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 483
1 Arun Vasu 315
2 OriginalGriff 250
3 Maciej Los 218
4 Aarti Meswania 170
0 Sergey Alexandrovich Kryukov 9,670
1 OriginalGriff 7,409
2 CPallini 3,968
3 Rohan Leuva 3,352
4 Maciej Los 2,861


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 18 Jul 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid