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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help writing a for each loop

Status
Not open for further replies.

JJ297

Programmer
Joined
Jun 27, 2007
Messages
30
Location
US
Can someone help me write a for each loop?

I'm using a data reader to read three fields I have in the database:

Topics, questions and answers

I would like to use a for each loop (or whatever is best) to get all topics from db and have all questions and answers pertaining to that topic underneath the topic.

The way the code is set up here it pulls the data but it's placed all in the same row.

Here's the code, THANKS!


Dim cmd As New Data.SqlClient.SqlCommand

Dim con As New Data.SqlClient.SqlConnection

Dim rd As Data.SqlClient.SqlDataReader = Nothing

Dim sb As New StringBuilder

Dim row As Web.UI.WebControls.TableRow

Dim cell1 As Web.UI.WebControls.TableCell

Dim cell2 As Web.UI.WebControls.TableCell

Dim cell3 As Web.UI.WebControls.TableCell


Try

con.ConnectionString = configurationManager.ConnectionStrings("xplorConnectionString2").ConnectionString

con.Open()

sb.Append("SELECT Topic, Question, Answer FROM QuesNAns")

cmd.Connection = con

cmd.CommandType = Data.CommandType.Text

cmd.CommandText = sb.ToString

rd = cmd.ExecuteReader

sb.Remove(0, sb.Length)

If Not (rd Is Nothing) Then
Do While (rd.Read) = True


row = New Web.UI.WebControls.TableRow()
cell1 = New Web.UI.WebControls.TableCell()
cell1.Controls.Add(New LiteralControl(rd.GetString(0)))

row = New Web.UI.WebControls.TableRow()
cell2 = New Web.UI.WebControls.TableCell()
cell2.Controls.Add(New LiteralControl(rd.GetString(1)))

cell3 = New Web.UI.WebControls.TableCell()
cell3.Controls.Add(New LiteralControl(rd.GetString(2)))
row.Cells.Add(cell1)
row.Cells.Add(cell2)
row.Cells.Add(cell3)
Table1.Rows.Add(row)

Loop

End If

Catch ex As Exception

'Display the full description of the error

Response.Write(ex.StackTrace.ToString & "<br>" & ex.Message.ToString & "<br>")

Finally

If Not (rd Is Nothing) Then

If Not rd.IsClosed Then rd.Close()

End If

If Not (rd Is Nothing) Then

If Not rd.IsClosed Then rd.Close()

End If

If Not (cmd Is Nothing) Then cmd.Dispose()

If Not (con Is Nothing) Then

If con.State = Data.ConnectionState.Open Then con.Close()

con.Dispose()

End If

End Try



End Sub

 
Seems like using nested Repeaters would be the way to go for what you are trying to do.
 
Yes I have a repeater on the page and it's working. I will look into nesting repeaters. Thanks!
 
no problem.. search this forum.. I know there have been some threads on nested repeaters.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top