Home > Software engineering >  How to get a List values in VB.NET? (GridView works well but I need to access each value instead)
How to get a List values in VB.NET? (GridView works well but I need to access each value instead)

Time:09-14

Here's my method which works very well if I bind it in a GridView as follows: GridView1.DataSource = Me.GetTags

Partial Class backoffice_posts_Edit
    Private Function GetTags() As List(Of Tag)
        Dim constring As String = ConfigurationManager.ConnectionStrings("AccessConnectionString1").ConnectionString
        Using con As New SqlConnection(constring)
            Using cmd As New SqlCommand("SELECT tag_id FROM tags_tbl WHERE tag_choice = 1", con)
                Dim tags As New List(Of Tag)
                cmd.CommandType = CommandType.Text
                con.Open()
                Using sdr As SqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        tags.Add(New Tag With {
                                      .tagId = Convert.ToInt32(sdr("tag_id"))
                                    })
                    End While
                End Using
                con.Close()
                Return tags
            End Using
        End Using
    End Function
End Class

Public Class Tag
    Public Property tagId As Integer
End Class

But instead of using GridView, I need to loop through the List values (to later apply a condition if a tag value is for example 23....)

So I did the following but unfortunately this prints tag;tag;tag;tag;tag;tag; and not the values:

For Each element In Me.GetTags
    Response.Write(element)
    Response.Write(";")
Next

CodePudding user response:

For Each element In Me.GetTags
    Response.Write(element.tagId)
    Response.Write(";")
Next

or

For Each tagId In Me.GetTags().Select(Function(t) t.tagId)

This is the correct answer from @41686d6564standsw.Palestine

  • Related