Click to See Complete Forum and Search --> : While loop problem - vb.net
mattastic
02-02-2010, 04:28 AM
Could I use this while loop to test for matching ID's and to build a submenu?
while (reader.Item("subpageid").ToString() = reader.Item("pageid").ToString())
submenu &= "<p>" & reader.Item("subtitle") & "</p>"
end while
The page wont seem to load when I try and browse to it .
Thanks in advance
ryanbutler
02-02-2010, 08:57 AM
How is the data base structured? You could have one table for the main links, then have a child table, for all the sub links, with a foreign key relationship to the main links. Then, you could do a query on associated id's and loop through it like that.
mattastic
02-02-2010, 09:14 AM
Hi ryan, thanks for your reply.
Thats what I've done.
Here is my sql:
SELECT subpages.subpageid, pages.pageid, sites.sitename, sites.siteid, pages.siteid,pages.pagetitle, subPages.subtitle FROM pages
LEFT JOIN sites ON pages.siteid = sites.siteid
LEFT JOIN subpages ON subpages.subpageid= pages.pageid
WHERE sites.siteID = 1 ORDER BY sites.siteid, pages.pageid, subpages.subpageid ASC
WHich gives me the following data:
subpageid pageid sitename siteid siteid pagetitle subtitle
NULL 1 SCHS 1 1 Sandwell Community Healthcare Services NULL
NULL 2 SCHS 1 1 About Us NULL
NULL 3 SCHS 1 1 Your Services NULL
4 4 SCHS 1 1 Equality and Diversity Team at SCHS 1111
4 4 SCHS 1 1 Equality and Diversity Team at SCHS 2222
4 4 SCHS 1 1 Equality and Diversity Team at SCHS 333
4 4 SCHS 1 1 Equality and Diversity Team at SCHS 44444
NULL 5 SCHS 1 1 Single Equality Scheme NULL
NULL 6 SCHS 1 1 Diversity Strands NULL
NULL 7 SCHS 1 1 Equality Impact Assessments NULL
NULL 8 SCHS 1 1 Quality and Safety Committee NULL
NULL 9 SCHS 1 1 Contact Us NULL
I'd like to loop through the data above and create subpages within pages:
pagetitle
pagetitle
subtitle
subtitle
pagetitle
pagetitle
etc etc
Using the datareader I have this code in vb:
While reader.Read()
' set title
sitename = reader.Item("sitename")
' set pagetitles
mydata &= "<p>" & reader.Item("pagetitle") & "</p>"
'check for matching ID's then output subtitle
' if subpages exist create submenu
if (reader.Item("subtitle").tostring() <> "")
' loop through subpages
while (reader.Item("subtitle").tostring() <> "")
submenu &= "<p>" & reader.Item("subtitle") & "</p>"
end while
'concatenate subpages to main data
mydata &= submenu
End if
End while
I think the nested while loop is the problem, do you know how I can get it to read the next item?
ryanbutler
02-02-2010, 04:14 PM
Hard to tell from my end. However, can you do a break point on that while loop and step through it to see what it's doing? That might give you a better idea of what's happening. See if something on your logic is off.