how to open multiple recordset inside loop in ASP.Net

Posted by Thiru under Regular Expressions on 3/19/2011 | Points: 10 | Views : 3456 | Status : [Member] | Replies : 1
How to open multiple recordset inside loop in ASP.Net
Usually i do it in Classic-ASP (for your information i have furnished my ASP code here:)
Please take a look on the below ASP code and let me know how to do the same in ASP.NET3.5
Expecting your valuable reply.

DB structure:
table name :tblcustomer
field 1 : name
field 2 : city
field 3 : referredby

Table Data:
name		city			referredby
thiru chennai funda
A pune thiru
B mumbai thiru
C pune A
D pune C
E delhi A

From the above data - i can show the referrence chain of a customer "D" in Classic ASP as:

Assume that i have opened the DB connection (conn) and declared recordset (A-sql,B-sql,C-sql)

set A-sql=conn.execute("select * from tblcustomer where name='D'")
do until A-sql.eof

response.write("Name: " & A-sql("name") & " referredby: " & A-sql("referredby")) & "<br>"

set B-sql=conn.execute("select * from tblcustomer where name='" & A-sql("referredby") & "'")
do until B-sql.eof

response.write("Name: " & B-sql("name") & " referredby: " & B-sql("referredby")) & "<br>"

set C-sql=conn.execute("select * from tblcustomer where name='" & B-sql("referredby") & "'")
do until C-sql.eof

response.write("Name: " & C-sql("name") & " referredby: " & C-sql("referredby")) & "<br>"



Output is:
Name: D referredby: C
Name: C referredby: A
Name: A referredby: thiru


Posted by: Raja on: 3/19/2011 [Member] Starter | Points: 25

Hi Thiru,

The same can be done in ASP.NET as well. You can follow the similar approach and write the ADO.NET code.

You can use the SqlDataAdapter to fetch the data from database to the DataTable, loop through the DataTable and inside again use the same connection to fetch the data from the database.

Just to show how to fetch the data from database, read BindData method of this article


Raja, USA

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response