DataSets -- Looping?!?!

7331

New Member
Hello all,<BR><BR>Can anyone tell me how to loop through a dataset and just grab the info you want? For example, I have a web page where the person clicks on a book title to see the details. Two records are returned since the table relationships are title > authors. Obviously, I dont want to show 2 duplicates on the page with only different authors. I want to show one detail in the DataList and concat the authors so the page would like the example below:<BR><BR>Title Price Authors<BR>Hello! $10.95 Jones, Smith<BR><BR>Right now it looks like this:<BR><BR>Title Price Authors<BR>Hello! $10.95 Jones<BR>Hello! $10.95 Smith<BR><BR><BR>How can you do this using vb.net and a datalist as the web form? <BR><BR>Thanks!!<BR>>Clint<BR>You can loop through a dataset with a for/next loop. If you are looking for books by a particular author the following should help get there. This assumes that the authors names are in column 0 of the dataset and the book names are in column 1.<BR><BR>If you are trying to find all books for each author you can add an outter for/next loop that loops through each author in your authors dataset.<BR><BR>For i = 0 to objDataSet.Tables("Authors").Rows.Count -1<BR><BR> strAuthName = objDataSet.Tables("Authors").Rows(i)(0)<BR><BR> For i = 0 to objDataSet.Tables("Books").Rows.Count -1<BR><BR> If objDataSet.Tables("Books").Rows(i)(0) = strAuthName then<BR><BR> strBookName = objDataSet.Tables("Books").Rows(i)(1)<BR><BR> '---Add strBookName to your array or list here<BR><BR> End If<BR> <BR> Next<BR><BR>Next<BR>This looks great - thanks! One follow up though: How do I reference that in the datalist on the aspx page? Right now I just have the code <%# DataBinder.Eval(Container,"DataItem.Title") %><BR><BR>Thanks again for the insight.If you read your results from the looping into variables or an array, you can just add them as ListItems to the ListBox.<BR><BR><ASP:ListBox ID="lbxBooks" RunAt="Server" ><BR><BR> <ASP:ListItem> varBook1 </ASP:ListItem><BR> <ASP:ListItem> varBook2 </ASP:ListItem><BR> <ASP:ListItem> varBook3 </ASP:ListItem> <BR><BR></ASP:ListBox><BR><BR>Another way to go would be to use a DataList Server Control with the RepeatDirection property set to "Horizontal". With a little work with ItemTemplates you could have the authors names displayed, the expand the list to show books when an author is clicked. A server control would also allow you to bind the control to a data source such as a dataset. It would take a little research on the control, but it might look nice.<BR><BR>Good Luck.<BR>
 
Back
Top