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 helping writing a for each loop

Status
Not open for further replies.

JJ297

Programmer
Joined
Jun 27, 2007
Messages
30
Location
US
I'm a newbie and looking for help in writing a for each loop in .Net.

The fields are Topic, question and answers. I'm trying to have only one topic listed and then the right questions and answers underneath each topic.

Can someone please help?
 
What are your objects?

When you use a for/each loop, you need to initialize objects.

Code:
Dim TB as New TextBox

For Each TB in Me.Controls
   Messagebox.Show (TB.Text)
Next

Show us some code and I'm sure one of the experts will be able to help you.



Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Thanks for your help...

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.

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

 
This is an asp.net question please post it in that forum.

BTW using one big try catch will kill performance.

And perhaps you should also state what is wrong with the above, orelse what doesn't work.

Christiaan Baes
Belgium

"My old site" - Me
 
Thanks Chrissie1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top