|
 |
asp_web_howto thread: Recursive subroutine call with ADO, update
Message #1 by Paul R Stearns <pauls@c...> on Mon, 28 Oct 2002 12:01:22 -0500
|
|
I forgot to put the where clause on the select.
I am writing a pure ASP/VBScript/JavaScript treeview system which is driven by a
database. In order to display the tree, my thought was to use a recursive
subroutine to traverse the parent child relationship in the database.
Before I start to write the code, I can think of a problem which is defining my
recordset names. If I am in the middle of looping through a recordset for one
group of records, and I call myself, how do I open another recordset? My
assumption is that I need to reference each recordset uniquely.
Consider a table with the fields ID, Parent, Action.
The call to the subroutine mifght be
call DisplayTree("root",0)
the subroutine might be
Sub DisplayTree(parent,level)
level = level + 1
set RsTree = Server.CreateObject("ADODB.Recordset")
RsTree.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=\myweb\mydb.mdb"
RsTree.Source = "SELECT * FROM TREEVIEW WHERE PARENT = '" & parent & "'" '
updated line
RsTree.Open()
While (NOT rsTree.EOF)
if RsTree("Action") = "folder"_
then
call DisplayTree(RsTree("ID"),level) ' this is the recursive bit
else
'do some ASP type stuff...
end if
rsTree.MoveNext()
Wend
RsTree.Close()
End Sub
What I forsee as my problem, is that I have the recordset RsTree open, and I
call myself which will attempt to reopen the recordset, which I think will fail.
Any ideas on how to change the name of the cursor for each iteration (level) of
the call.
Paul
Message #2 by Paul R Stearns <pauls@c...> on Mon, 28 Oct 2002 14:58:06 -0500
|
|
It would appear that nobody was paying attention but, this works;
Sub DisplayTree(parent,level)
level = level + 1
set RsTreeView = Server.CreateObject("ADODB.Recordset")
RsTreeView.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=\myweb\MyDB.mdb"
RsTreeView.Source = "SELECT * FROM TREEVIEW WHERE UCase(PARENT) = '" &
UCase(parent) & "' ORDER BY CAPTION"
RsTreeView.Open()
While (NOT RsTreeView.EOF)
if UCase(RsTreeView("Action")) = "<FOLDER>"_
then
Response.write String(Level,"o") & RsTreeView("Caption") & "<br>" & chr(13)
call DisplayTree(RsTreeView("Caption"),level) ' this is the recursive bit
else
Response.write String(Level,"*") & RsTreeView("Caption") & "<br>" & chr(13)
end if
RsTreeView.MoveNext()
Wend
RsTreeView.Close()
level = level - 1
End Sub
Call this routine and this is what you get;
oRoot Item - 1
ooLevel One - 1
oooLevel Two - 1
***Level Two - 2
ooLevel One - 2
***Level Two - 3
*Root Item - 2
*Root Item - 3
This code may solve a problem for you someday, or it may be good budgie cage liner,
but in any case I hope it's useful for something...
Paul
Paul R Stearns wrote:
> I forgot to put the where clause on the select.
>
> I am writing a pure ASP/VBScript/JavaScript treeview system which is driven by a
>
> database. In order to display the tree, my thought was to use a recursive
> subroutine to traverse the parent child relationship in the database.
>
> Before I start to write the code, I can think of a problem which is defining my
> recordset names. If I am in the middle of looping through a recordset for one
> group of records, and I call myself, how do I open another recordset? My
> assumption is that I need to reference each recordset uniquely.
>
> Consider a table with the fields ID, Parent, Action.
>
> The call to the subroutine mifght be
>
> call DisplayTree("root",0)
>
> the subroutine might be
>
> Sub DisplayTree(parent,level)
>
> level = level + 1
>
> set RsTree = Server.CreateObject("ADODB.Recordset")
> RsTree.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data
> Source=\myweb\mydb.mdb"
> RsTree.Source = "SELECT * FROM TREEVIEW WHERE PARENT = '" & parent & "'" '
> updated line
> RsTree.Open()
>
> While (NOT rsTree.EOF)
> if RsTree("Action") = "folder"_
> then
> call DisplayTree(RsTree("ID"),level) ' this is the recursive bit
> else
> 'do some ASP type stuff...
> end if
> rsTree.MoveNext()
> Wend
>
> RsTree.Close()
> End Sub
>
> What I forsee as my problem, is that I have the recordset RsTree open, and I
> call myself which will attempt to reopen the recordset, which I think will fail.
>
> Any ideas on how to change the name of the cursor for each iteration (level) of
> the call.
>
> Paul
>
> ---
>
> Improve your web design skills with these new books from Glasshaus.
>
> Usable Web Menus
> http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
> r-20
> Constructing Accessible Web Sites
> http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
> r-20
> Practical JavaScript for the Usable Web
> http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
> r-20
|
|
 |