Search code examples
sqldatabaseasp-classicado

How to remove/fix repeating records in Classic asp


I am trying to show all the companies that are associated with a specific ID (practice_Area_ID). I cant figure out whether I am setting up my Do Loop incorrect or if it is a problem with my SQL code.

The problem is the results that are displayed are only showing one company (when there should be 5 different ones) and it is duplicated multiple times depending on how many of the practice areas were found (5 in this instance).

In other words, the problem is that instead of displaying 5 different companies I am only seeing one company but it is listed 5 times.

Ignore my terrible table formatting...just trying to get the results to display here.

<%
intPractice_Area_ID = Request.QueryString("intPractice_Area_ID")
%>

(Random html here)

<%

Set rsCompInfo = Server.CreateObject ("ADODB.Recordset")
compSQL = "SELECT Company_Main.Company_Name, Company_Main.Company_address, Practice_Areas.Practice_Area_ID FROM Practice_Areas INNER JOIN (Company_Main INNER JOIN Company_Practice_CK ON Company_Main.Company_ID = Company_Practice_CK.Company_ID) ON Practice_Areas.Practice_Area_ID = Company_Practice_CK.Practice_Area_ID WHERE Company_Practice_CK.Practice_Area_ID =" & intPractice_Area_ID  
rsrsCompInfo.Open compSQL, Conn
strName = rsProdInfo("Company_Name")
strAddress = rsProdInfo("Company_Address")

%>

<table width="200" border="1">
    <tr>
        <th>Firm Name</th>
        <th>Address</th>

    </tr>

<%


if rsCompInfo.EOF then
Response.Write "No Records Found!"
else
strName = rsCompInfo("Company_Name")
strAddress = rsCompInfo("Company_Address")

Do While Not rsCompInfo.EOF
%>


<table border="1" cellspacing="0" cellpadding="2" width="602">
     <tr>

         <td width="560" valign="top"><font face="Verdana" size="3"><b><%= strName %></b></font><p>
        <td width="560" valign="top"><font face="Verdana" size="3"><b><%= strAddress %></b></font><p>         
            <input type="hidden" name="intPractice_Area_ID" value="<%= intPractice_Area_ID %>">
     </tr>

         <td width="34"><td width="560"><font face="Verdana" size="2">
 </table>
<%
rsCompInfo.MoveNext
Loop

end if

rsCompInfo.Close
set rsCompInfo = Nothing

Conn.Close
set Conn = Nothing

Solution

  • This is absolutely correct:

    Do While Not rsCompInfo.EOF
      ...
      rsCompInfo.MoveNext
    Loop
    

    But you need to read the current values of rsCompInfo each time you go through the loop:

      <%= rsCompInfo("Company_Name") %>
      <%= rsCompInfo("Company_Address") %>