vendredi 11 juin 2021

List is empty outside of function even after adding the list outside

Why is my list count 0 outside of PopulateNames()?

I have a list at the top of my vb.net code:

    Private defaultAvailable As List(Of ListItem) = New List(Of ListItem)()

I can add items to my list here:

'Populate cbNames with all names
Private Sub PopulateNames()
    Using sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("databasehere").ConnectionString)
        sqlConn.Open()

        Using sqlCmd As New SqlCommand()
            sqlCmd.CommandText = "select [ID], [FirstName] from vwNames"
            sqlCmd.Connection = sqlConn

            Using sdr As SqlDataReader = sqlCmd.ExecuteReader()
                While sdr.Read()
                    Dim item As New ListItem()
                    item.Text = sdr("FirstName").ToString()
                    item.Value = sdr("ID").ToString()
                    cbNames.Items.Add(item)

                    defaultNames.Add(item)

                End While
            End Using
        End Using



Aucun commentaire:

Enregistrer un commentaire