Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through 2 recordsets - part 2 - arrays ?

Status
Not open for further replies.

welshone

Programmer
Jul 30, 2001
414
GB
Hello,
earlier I asked for help regarding looping through 2 recordsets, lobstah came up with the following solution, which works, however this way is storing a connection object - an OPEN connection object at that - in a session variable!.
Does anybody have any other ideas ?
how would I run the first sql, put the results into an array, then close that connection, then open the second connection and run the second sql against the results in the array. ?



<%
Dim SearchTextcompany, MyConncompany, SQLcompany, RScompany

Set MyConncompany=Server.CreateObject(&quot;ADODB.Connection&quot;)
MyConncompany.Open &quot;schoolsasp&quot;,&quot;schoolsasp&quot;,&quot;schoolsasp&quot;

SQLcompany = &quot;SELECT EstablishmentID, DfEE, EstablishmentName From Establishments ORDER BY EstablishmentName&quot;

Set RScompany = MyConncompany.Execute(SQLcompany)
%>

<%
Dim module
Dim sError

Param = Request.QueryString(&quot;Param&quot;)
Data = Request.QueryString(&quot;Data&quot;)
%>

<center>

<TABLE BORDER=1 BGCOLOR=#ffffff CELLSPACING=1 CELLPADDING=3><FONT FACE=&quot;Arial&quot; COLOR=#000000>

<THEAD>
<TR>
<TH BGCOLOR=#008080 BORDERCOLOR=#000000 nowrap><FONT SIZE=2 FACE=&quot;Arial&quot; COLOR=white>Establishment</FONT></TH>
<TH BGCOLOR=#008080 BORDERCOLOR=#000000 nowrap><FONT SIZE=2 FACE=&quot;Arial&quot; COLOR=white>Filename</FONT></TH>

</TR>
</THEAD>
<TBODY>

<%

Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.open &quot;Holding&quot;,&quot;edholding&quot;,&quot;edholding&quot;
Set Session(&quot;Holding_conn&quot;) = conn

do while Not rscompany.eof

module = Server.HTMLEncode(rscompany.Fields(&quot;dfee&quot;).Value)

spCommand.Parameters.Append spCommand.CreateParameter (&quot;@module&quot;, 200, 1, 50, module)

sql= &quot;files&quot;


sql = &quot;SELECT DISTINCT [RepGenA].[filename] FROM RepGenA WHERE SUBSTRING(RepGenA.filename,4,4) = '&quot;&module&&quot;' &quot;


If cstr(Param) <> &quot;&quot; And cstr(Data) <> &quot;&quot; Then
sql = sql & &quot; And [&quot; & cstr(Param) & &quot;] = &quot; & cstr(Data)
End If
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open sql, conn, 3, 3

if rs.eof then
sError = &quot;No Files Uploaded.&quot;
else
SError=&quot;&quot; 'clear error
RS.movefirst
do while Not rs.eof


%>

<TR VALIGN=TOP onMouseover=&quot;changeto('yellow')&quot;
onMouseout=&quot;changeback('white')&quot;>

<TD BORDERCOLOR=#c0c0c0 nowrap><FONT SIZE=2 FACE=&quot;Arial&quot; COLOR=#000000><a href=&quot;./viewschool.asp?Establishment=<%=Server.HTMLEncode(rscompany.Fields(&quot;EstablishmentID&quot;).Value)%>&quot;><%=Server.HTMLEncode(rscompany.Fields(&quot;DfEE&quot;).Value)%>, <%=Server.HTMLEncode(rscompany.Fields(&quot;establishmentname&quot;).Value)%></A><BR></FONT></TD>

<TD BORDERCOLOR=#c0c0c0 nowrap><FONT SIZE=2 FACE=&quot;Arial&quot; COLOR=#000000><a href=&quot;./moredetails.asp?Filename=<%=Server.HTMLEncode(rs.Fields(&quot;filename&quot;).Value)%>&quot;><%=Server.HTMLEncode(rs.Fields(&quot;filename&quot;).Value)%></a><BR></FONT></TD>

</TR>


<%
rs.MoveNext
loop
end if
rs.close
rscompany.MoveNext
loop%>


</TBODY>
<TFOOT></TFOOT>
</TABLE>

</BODY>


<%
conn.close
set RS = Nothing
set Conn = Nothing
%>

<%
RScompany.Close
MyConncompany.Close
Set RScompany = Nothing
Set MyConncompany = Nothing
%>
<br>


 
Dim arrRows

Set MyConncompany=Server.CreateObject(&quot;ADODB.Connection&quot;)
MyConncompany.Open &quot;schoolsasp&quot;,&quot;schoolsasp&quot;,&quot;schoolsasp&quot;

SQLcompany = &quot;SELECT EstablishmentID, DfEE, EstablishmentName From Establishments ORDER BY EstablishmentName&quot;

RScompany.Open sSQL, MyConncompany, 0, 1
arrRows = RScompany.GetRows
RScompany.Close
Set RScompany = Nothing


' arrRows is now a 2 dimensional array, 2nd dimension being record # offset, 1st dimension being field # offset:


arrRows(0,0) ' Record 1, field 1
arrRows(1,0) ' Record 1, field 2
arrRows(2,0) ' Record 1, field 3
arrRows(3,0) ' Record 1, field 4
arrRows(4,0) ' Record 1, field 5
arrRows(0,1) ' Record 2, field 1
arrRows(1,1) ' Record 2, field 2
arrRows(2,1) ' Record 2, field 3
arrRows(3,1) ' Record 2, field 4
arrRows(4,1) ' Record 2, field 5

etc.
and you can loop thru it like this:
(example)
Dim r, f
For r = 0 To UBound(arrRows,2) ' loop thru records
For f = 0 To UBound(arrRows,1) ' loop thru fields
Response.Write &quot;<td>&quot; & arrRows(f,r) & &quot;</td>&quot;
Next
Next

 
thats great lobstah, but how do I then use the results from this recordset in the 2nd recordset ?
 
instead of your
do while not recordset.eof
.....
loop

use the

dim i, EstablishmentID, DfEE, EstablishmentName
for i=0 to UBound(arrRows,2)
EstablishmentID = arrRows(0,i)
DfEE = arrRows(1,i)
EstablishmentName = arrRows(2,i)
'then add your 2nd recordset code in here.......

next

is that enough to get you going?

 
I think so,
however I get object required message, relating to this line :

RScompany.Open SQLCompany, MyConncompany, 0, 1

 
try using what you had originally in place....

Set RScompany = MyConncompany.Execute(SQLcompany)
 
Yeah, that seemed to work..

I think this is the last question, then Hopefully it will work,.
I cna't use this anymore :
<TD BORDERCOLOR=#c0c0c0 nowrap><FONT SIZE=2 FACE=&quot;Arial&quot; COLOR=#000000><a href=&quot;./viewschool.asp?Establishment=<%=Server.HTMLEncode(rscompany.Fields(&quot;EstablishmentID&quot;).Value)%>&quot;><%=Server.HTMLEncode(rscompany.Fields(&quot;DfEE&quot;).Value)%>, <%=Server.HTMLEncode(rscompany.Fields(&quot;establishmentname&quot;).Value)%></A><BR></FONT></TD>

what should I use instead of Server.HTMLEncode ?
 
<TD BORDERCOLOR=#c0c0c0 nowrap><FONT SIZE=2 FACE=&quot;Arial&quot; COLOR=#000000><a href=&quot;./viewschool.asp?Establishment=<%=Server.HTMLEncode(EstablishmentID)%>&quot;><%=Server.HTMLEncode(DfEE)%>, <%=Server.HTMLEncode(establishmentname)%></A><BR></FONT></TD>


that should work. you can still use Server.HTMLEncode...
 
Sorry lobstah,
it is still so slow.
I have tried to change it so both queries are created into an array, meaning the server isn't put under too much processing., however I get the following message :

ADODB.Recordset error '800a0e7d'
The connection cannot be used to perform this operation. It is either closed or invalid in this context.

/HoldingDBv2/searchresultsschool.asp, line 345

however it does show the first record....


my code now looks like :




<%
Dim SearchTextcompany, MyConncompany, SQLcompany, RScompany

Dim arrRows

Set MyConncompany=Server.CreateObject(&quot;ADODB.Connection&quot;)
MyConncompany.Open &quot;schoolsasp&quot;,&quot;schoolsasp&quot;,&quot;schoolsasp&quot;

SQLcompany = &quot;SELECT EstablishmentID, DfEE, EstablishmentName From Establishments ORDER BY EstablishmentName&quot;

Set RScompany = MyConncompany.Execute(SQLcompany)

arrRows = RScompany.GetRows

RScompany.Close
Set RScompany = Nothing
MyConncompany.Close
Set MyConncompany = Nothing

%>

<%
Dim module
Dim sError

Param = Request.QueryString(&quot;Param&quot;)
Data = Request.QueryString(&quot;Data&quot;)
%>

<center>

<TABLE BORDER=1 BGCOLOR=#ffffff CELLSPACING=1 CELLPADDING=3><FONT FACE=&quot;Arial&quot; COLOR=#000000>

<THEAD>
<TR>
<TH BGCOLOR=#008080 BORDERCOLOR=#000000 nowrap><FONT SIZE=2 FACE=&quot;Arial&quot; COLOR=white>Establishment</FONT></TH>
<TH BGCOLOR=#008080 BORDERCOLOR=#000000 nowrap><FONT SIZE=2 FACE=&quot;Arial&quot; COLOR=white>Filename</FONT></TH>

</TR>
</THEAD>
<TBODY>

<%

Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.open &quot;Holding&quot;,&quot;edholding&quot;,&quot;edholding&quot;
Set Session(&quot;Holding_conn&quot;) = conn

dim i, EstablishmentID, DfEE, EstablishmentName
for i=0 to UBound(arrRows,2)
EstablishmentID = arrRows(0,i)
DfEE = arrRows(1,i)
EstablishmentName = arrRows(2,i)

'module = Server.HTMLEncode(rscompany.Fields(&quot;dfee&quot;).Value)


sql = &quot;SELECT DISTINCT [RepGenA].[filename] FROM RepGenA WHERE SUBSTRING(RepGenA.filename,4,4) = '&quot;&DfEE&&quot;' &quot;


If cstr(Param) <> &quot;&quot; And cstr(Data) <> &quot;&quot; Then
sql = sql & &quot; And [&quot; & cstr(Param) & &quot;] = &quot; & cstr(Data)
End If
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open sql, conn, 3, 3

arrRows1 = RS.GetRows

RS.Close
Set RS = Nothing
Conn.Close
Set Conn = Nothing

dim j, Filename
for j=0 to UBound(arrRows1,2)
Filename = arrRows1(0,j)


' if rs.eof then
'sError = &quot;No Files Uploaded.&quot;
'else
' SError=&quot;&quot; 'clear error
' RS.movefirst
'do while Not rs.eof

%>

<TR VALIGN=TOP onMouseover=&quot;changeto('yellow')&quot;
onMouseout=&quot;changeback('white')&quot;>

<TD BORDERCOLOR=#c0c0c0 nowrap><FONT SIZE=2 FACE=&quot;Arial&quot; COLOR=#000000><a href=&quot;./viewschool.asp?Establishment=<%=Server.HTMLEncode(EstablishmentID)%>&quot;><%=Server.HTMLEncode(DfEE)%></a></FONT></TD>

<TD BORDERCOLOR=#c0c0c0 nowrap><FONT SIZE=2 FACE=&quot;Arial&quot; COLOR=#000000><a href=&quot;./moredetails.asp?Filename=<%=Server.HTMLEncode(filename)%>&quot;><%=Server.HTMLEncode(filename)%></a><BR></FONT></TD>

</TR>

<%

next
'end if

next
%>
 
If speed is an issue, then I'll repeat what I mentioned in your first post on this issue:

I also think that you can combine this into one select statement:
Code:
sql = &quot;SELECT DISTINCT [RepGenA].[filename], EstablishmentID, DfEE, EstablishmentName FROM RepGenA, Establishments WHERE SUBSTRING(RepGenA.filename,4,4) = DfEE&quot; 

If cstr(Param) <> &quot;&quot; And cstr(Data) <> &quot;&quot; Then
        sql = sql & &quot; And [&quot; & cstr(Param) & &quot;] = &quot; & cstr(Data)
End If
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open sql, conn, 3, 3
Then you don't need to loop thru 2 recordsets.....

I really think that would be your best solution, and cleanest in every sense.
 
The thing is its 2 databases, so I need 2 connections.
 
oh, yeah. sorry about that......

speed issues:
are they large databases/tables? are your queries returning a large amount of data? the server(s) that the databases reside on, are they overly busy?

 
one DB - ois quite small, returning about 400 records.
the other database returns about 500,000 records
 
i'm not real good with sql server, but maybe you can create stored procedures and run those, try posting on the sql server forum to get more info on that. i think that will greatly improve speed and streamline your code too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top