Nested loop logic problem

I cant get my nested loop to output the right code:

If pagesreader.Read() Then

            ' if subtitle exists then output
        mydata = "<h2>" & pagesreader.Item("documentcategoryname") & "</h2>"
       
     
        
        While pagesreader.Read()
           
            mydata &= "<h2>" & pagesreader.Item("documentcategoryname") & "</h2>"

            If (pagesreader.Item("documentcat") = pagesreader.Item("documentcategoryid")) Then
                
                mydata &= "<p>" & pagesreader.Item("documentname") & "</p>"

            End If
            
        
        End While
        
    End If

produces:

About Us

About Us
doccy4

Map Of Medicine
doccy5

When I need it to procude:

About Us

doccy3
doccy4

Map Of Medicine
doccy5

Can anyone please help me?

Thankyou

That’s because your if statement and your while mean you’re starting with the second record of the recordset. Removing the if statement (which really isn’t needed) should solve the issue.

You will want to add some sort of logic with a temp variable to ensure you’re not showing the category name over and over again - only when it changes.

Thanks very much for your reply.

Can you please tell what logic I need for the temp variable, it keeps repeating the category name, I can’t get it working for love nor money.

Thanks again

I typically write in C#, so there may be syntax errors, but this would be the general gyst


dim CategoryName as String = ""
While pagesreader.Read()
 If (pagesreader.Item("documentcategoryname") <> CategoryName) then
  mydata &= "<h2>" & pagesreader.Item("documentcategoryname") & "</h2>"
 End If
 If (pagesreader.Item("documentcat") = pagesreader.Item("documentcategoryid")) Then
  mydata &= "<p>" & pagesreader.Item("documentname") & "</p>"
 End If
End While

The one question I have is this line:
If (pagesreader.Item(“documentcat”) = pagesreader.Item(“documentcategoryid”)) Then

What is that supposed to do? Comparing data within the same row seems odd to me - you should be able to filter that out via your sql query.

Thanks again.

Do I need to set the CategoryName variable to something? With your code isnt it always set to “”?

The second if statement was my attempt at hiding the category name, which obviously failed.

Thanks again for your reply

Oops. Yep, there should be a line immediately the end if.
CategoryName = pagesreader.ItemCOLOR=#000000[/COLOR]

I kinda figured that was what you were trying to do, so you can remove the if statement around that statement since it’s not needed anymore.

Some good advice already from dave here. I just have one question for you. Is there a reason you are building up a string instead of using a server side control like a repeater? As that is what they are meant for. That is if you are writing this to the screen, which I would assume you are.

Thanks for all the replies and advice Dave.

Nightstalker - When I get chance I’ll look into it, thanks for the advice