Nested selecting data from table

R

Rafi

Hi,

Can anyone tell me, how to solve this:

I have taCategory in my Sql Server database which contains fields
id_category, id_subcategory, name.
I need to get all subcategories of a main category.

i.e.

id_category id_subcategory name
1 1 Computers
2 2 Books
3 1 Software
4 1 Hardware
5 4 Motherboards
6 4 CPUs
7 6 Intel
8 6 AMD
9 3 Games

So when I choose id = 1 I want to have:

Computers
Software
Games
Hardware
Motherboards
CPUs
Intel
AMD

I wrote in C#

in Page_Load
{
writeAll("1");
}

private void writeAll( string id_cat )
{
SqlDataReader dr;
sqlCommand1.Parameter[1].Value = id; // sqlCommand1
is my Stored Procedure which has one input parameter @id

//( SELECT * FROM tbCategori WHERE id_subcategory = @id AND id_category <>
@id )

sqlConnection1.Open();
dr = sqlCommand1.ExecuterReader();

while( dr.Read())
{
Response.Write(dr.GetValue(2).ToString());
writeAll(dr.GetValue(0).ToString());
}
dr.Close();
sqlConnection1.Close();
}

The problem is when I want to use writeAll in a while loop, because it tells
me that my connection is open. I can
write if statement to avoid this, but later there is information that dr is
open. If I close it I cannot read data.

How to solve this, please help me.
It would be nice to store all that for example in a view in database. But I
never used views :(

Rafi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top