Nurul Nurul - 1 month ago 16
Vb.net Question

data in arraylist not displayed correctly. instead displays "System.Collections.ArrayList" - vb.net

Can anyone please help, tell me why the data from database did not display in the rows? Instead "System.Collections.ArrayList" and "System.Data.DataRow" are displayed.

Dim myArray(4, 4) As String
For Each dtrow As DataRow In dt.Rows
list.Add(dtrow)
Next

For i = 0 To myArray.GetUpperBound(0)
If i = 0 Then
html.Append("<tr>")
ElseIf (i > 0 And i < 5) Then
html.Append("</tr>")
html.Append("<tr>")
For j = 0 To myArray.GetUpperBound(1)
Dim no = 0
'insert the code i mentioned below here'
no += 1
Next ' j'
End If ' if in i'
Next ' i


When i tried running using this 1 line below inserted to the above code, it displays System.Collections.ArrayList in the rows.
html.Append(list)


But when i tried running using this 1 line below inserted to the above code, it displays System.Data.DataRow in the rows.
html.Append(list(no))


========================================================================

Another method i tried, when i tried running using this line "html.Append(dt.Rows.Item(0)(0))" , it displays the correct data, but when i tried to do a loop to increase the number of columns and rows, it shows the same value of data. as though as the 'rw' and 'col' loop didnt work.

For j = 0 To myArray.GetUpperBound(1)
Dim no = 0
For rw = 0 To 5
Dim rno = 0
For col = 0 To 5
Dim cno = 0
no += 1
html.Append(dt.Rows.Item(rno)(cno))
cno += 1
Next
rno += 1
Next
Next 'j'

Answer

You already got a correct result when you used html.Append(dt.Rows.Item(0)(0)) - now you need write correct loop.

Noticed that you have DataTable instance - use this for looping through all rows and columns

For Each row As DataRow in dt.Rows
    html.Append("<tr>")

    For Each column in dt.Columns
        html.Append("<td>")
        html.Append(row(column.Ordinal))
        html.Append("</td>")
    End For

    html.Append("</tr>")
End For

When working with xml I like using LINQ to Xml with XElement type, with those I can be sure that my xml structure is correct during compiling.
Visual basic have a nice feature which c# does not - XML Literals (Visual Basic)

Dim table As XElement = <table></table>

For Each row As DataRow in dt.Rows
    Dim tr As XElement = <tr></tr>

    For Each column in dt.Columns
        Dim td As XElement = <td></td>
        td.Value = row(column.Ordinal).ToString()

        tr.Add(td)
    End For

    table.Add(tr)
End For