How to print all values from a list of objects?

0

I am printing the contents of a list of objects that I retrieve from a DB, but when I read the result of the list, it only prints a single object and not all of the ones I store, I do not know what I am doing wrong, What I try to do is send an email with the records of a query, everything is fine just this is how to print all the objects in the list in the body of the email

Dim tabla As New funciones()
                Dim valor = tabla.Consultar()
                    For Each item As lstRecogerEquipos In valor
                        If Not valor Is Nothing Then
                            x = item.factura
                            y = item.fecha

                        End If

                    Next
                    cuerpoCorreo = "<html><body><div style='text-align:center'>" & _
    "<h3>Nueva Compra </h3><hr/><br/>" & _
    "<italic>Hola</italic><br/>" & _
    "<br/><table style='border-color: #666;margin: 0 auto;' cellpadding='10' >" & _
    "<tr style='background: #eee;'><td><strong>Factura:</strong> </td><td>" & x & "</td></tr>" & _
    "<tr><td><strong>Fecha:</strong> </td><td>" & y & "</td></tr>" & _
    "</table>" & _
    "<div></body></html>"
    
asked by Ivxn 21.06.2016 в 21:30
source

2 answers

3

The problem is that you are sending all the html at the end, so if you run the foreach , you probably only send the last data stored in x and y , try:

cuerpoCorreo = "<html><body><div style='text-align:center'>" & _
               "<h3>Nueva Compra</h3> <hr/>" & _
               "<br/><italic>Hola</italic><br/><br/>" & _
               "<table style='border-color: #666;margin: 0 auto;' cellpadding='10' >" & _
               "<tr><thead> <td>Factura</td> <td>Fecha</td> </thead></tr>"

Dim tabla As New funciones()
Dim valor = tabla.Consultar()

For Each item As lstRecogerEquipos In valor ' Recorremos todos los elementos
    If Not valor Is Nothing Then
        x = item.factura
        y = item.fecha
        ' Los agregamos al valor actual de la variable.
        cuerpoCorreo += "<tr> <td>" & x & "</td> <td>" & y & "</td> </tr>" 
    End If
Next

cuerpoCorreo += "</table></div></body></html>"

And by way of "bonus" , you can save a few lines of code by doing the following:

Note: only apply this if the value of x or y is not used after the cycle.

For Each item As lstRecogerEquipos In valor ' Recorremos todos los elementos
    If Not valor Is Nothing Then
        ' Envias el valor de item.factura y item.fecha directamente a cuerpoCorreo
        cuerpoCorreo += "<tr> <td>" & item.factura & "</td> <td>" & item.fecha & "</td> </tr>" 
    End If
Next

In this way you do not have to define the variables x and y , but as I said above, if you use those variables after the cycle, it is not convenient to do it in the last way.

    
answered by 21.06.2016 / 21:47
source
2

What happens is that x and y get new values in each cycle, and they only keep the values of the last cycle.

One solution would be to use a string that concatenates rows and columns within the iteration, and when it is finished, include the string that has the structure of the table, where it corresponds. For example:

If Not valor Is Nothing Then
    x = item.factura
    y = item.fecha
    cadenaTabla = cadenaTabla + "<tr style='background: #eee;'><td><strong>Factura:</strong> </td><td>" & x & "</td></tr>" & _"<tr><td><strong>Fecha:</strong> </td><td>" & y & "</td></tr>"
End If

And after the iteration finishes, in your body variable Mail:

<table style='border-color: #666;margin: 0 auto;' cellpadding='10' >" & _cadenaTabla & _"</table>"
    
answered by 21.06.2016 в 21:47