Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6154599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:17:25+00:00 2026-05-23T20:17:25+00:00

I am attempting to read the results from a SQL query into a List(Of)

  • 0

I am attempting to read the results from a SQL query into a List(Of) and I can see the List.count while adding items to the List increments, however in another part of my code when I am attempting to read the List the List.Count returns 0.

My List is as follows:

Public Class roomList
    Public Sub New()
    End Sub

    Public Property eName() As String
        Get
            Return _eName
        End Get
        Set(ByVal value As String)
            _eName = value
        End Set
    End Property
    Public Property eID() As Integer
        Get
            Return _eID
        End Get
        Set(ByVal value As Integer)
            _eID = value
        End Set
    End Property
    Public Property eEmail() As String
        Get
            Return _eEmail
        End Get
        Set(ByVal value As String)
            _eEmail = value
        End Set
    End Property
    Private _eID As Integer
    Private _eName As String
    Private _eEmail As String
End Class

I am reading the SQL results into the list as follows:
EDIT: this snippet of code is in it’s own Sub().

Dim rooms As New List(Of roomList)    
While dr.Read
      rooms.Add(New roomList() With {.eID = dr.Item("mId"), .eName = dr.Item("roomName"), .eEmail = dr.Item("roomEmail")})
      MsgBox(rooms.Count)
    End While

I am then attempting to read the count of the List as follows:
EDIT: this snippet of code is in it’s own Sub().

Dim rooms As New List(Of roomList)()
MsgBox(rooms.Count)

Can anyone help me to understand why the List is returning a count of 0, and help me to fix the code so that the list is being correctly called?

I appreciate any assistance anyone is able to offer and if there is any further information you require please dont hesitate in contacting me.

Matt

EDIT: The two snippets of code in which I am attempting to read/write the List are in their own Sub’s, should I instead be declaring a global variable?

The first Sub is as follows:

Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Try
            getRoomList()
            Dim rooms As New List(Of roomList)()
            Dim r As roomList
            MsgBox(rooms.Count)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Which calls a getRoomList() sub:

Private Sub getRoomList()
        Dim rooms As New List(Of roomList)
        Try
        ' SQL Query stuff goes here
                dr = sqlCmd.ExecuteReader
                While dr.Read
                    rooms.Add(New roomList() With {.eID = dr.Item("mId"), .eName = dr.Item("roomName"), .eEmail = dr.Item("roomEmail")})
                    'MsgBox(rooms.Count)
                End While
            End If
        Catch ex As Exception

        End Try
    End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:17:26+00:00Added an answer on May 23, 2026 at 8:17 pm

    This works:

    Dim rooms As New List(Of roomList)    
    While dr.Read      
    rooms.Add(New roomList() With {.eID = dr.Item("mId"), .eName = dr.Item("roomName"), .eEmail = dr.Item("roomEmail")})      
    MsgBox(rooms.Count)    
    End While
    MsgBox(rooms.Count)
    

    Eliminate the creation of a new room:

    Dim rooms As New List(Of roomList)()
    MsgBox(rooms.Count)
    

    That above creates a new list object, which is in fact initially empty.

    Per your edit

    You mentioned

    EDIT: The two snippets of code in which I am attempting to read/write
    the List are in their own Sub’s, should I instead be declaring a
    global variable?

    Because they are in 2 different sub routines, one instance is lost hence the count is now 0 because you’ve created a new instance. What you want to do is take the initial list and pass it to the sub routine / function in question. If you plan to modify that list pass it ByRef otherwise pass it ByVal. Something to this effect:

    'code...
     PrintListCount(rooms)
    'other code...
    
    protected Sub PrintListCount(ByVal rooms as List(Of roomList))
    MsgBox(rooms.Count)
    end sub
    

    This example is kind of scary as you really don’t need to pass an entire list by value to print the count of the list..but I am just using it to show you how to pass the list rooms.

    Per your second edit

    Your getRoomList() function doesn’t return a list. How do you expect to gather the count without a proper object returned? Change the sub to a function:

    Private Function getRoomList() As List(Of roomList)        
    Dim rooms As New List(Of roomList)        
    Try        ' SQL Query stuff goes here                
    dr = sqlCmd.ExecuteReader                
    While dr.Read                    
    rooms.Add(New roomList() 
    With {.eID = dr.Item("mId"), .eName = dr.Item("roomName"), .eEmail = dr.Item("roomEmail")})                    
    'MsgBox(rooms.Count)                
    End While            
    End If        
    Catch ex As Exception        
    End Try   
    return rooms 
    End Function
    

    And in your load event:

    Dim rooms as List(Of roomList)
    rooms = getRoomList();
    

    Now that you have a proper rooms object you can now say:

    MsgBox(rooms.Count)

    You will need to read up on passing objects, how functions work, and variable scope. Without this knowledge you are making it much harder on yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While attempting to read data from memory into an enum via the following code,
I am attempting to read from a url into a System.IO.Stream object. I tried
I'm attempting to use TinyXML to read and save from memory, instead of only
Attempting to insert an escape character into a table results in a warning. For
Attempting to print out a list of values from 2 different variables that are
I am attempting to read a MySQL database from my C# project using the
I'm attempting to read a value from a registry entry with Powershell. This is
I'm attempting to read a PDF from a UNC path, i.e. \10.32.16.24\repositories\repository0001\VOL00001\ktappb01_024655001_0.PDF My virtual
I'm attempting to read values from a 2-dimensional array and multiply them to make
I am attempting to read a large XML document and I wanted to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.