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