[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

All items in collection same as the last iteration.

Posted on 2006-05-17
2
Medium Priority
?
180 Views
Last Modified: 2010-04-23
When I put a watch on EmployeeCollection at the bottom of the sub, it shows that all items in the collection are the same as the last one read. Why is this? I've tried everything I can think of. What am I doing wrong? I included the code for the collection class in case that is the problem. "Employee1" is an instance of "Employee" class. I tried changing byval to byref in the collection class and it made no difference.
Please help.
Thanks.

Private Sub LoadEmployees()
        Dim cmdEmp As New SqlCommand
        Dim i As Int16 = 0
        Dim obj As Object
        cmdEmp.Connection = SqlConnection2
        cmdEmp.CommandText = "SELECT EmpNum, EmpFName,EmpLName, EmpPW, EmpInit " _
            & " FROM Employee  ORDER BY EmpNum  "
        Try
            SqlConnection2.Open()
            Dim rdrEmp As SqlDataReader = cmdEmp.ExecuteReader()
            Do While rdrEmp.Read()

                Employee1.Number = CType(rdrEmp("EmpNum"), Integer)
                Employee1.FirstName = CType(rdrEmp("EmpFName"), String)
                Employee1.LastName = CType(rdrEmp("EmpLName"), String)
                Employee1.Password = CType(rdrEmp("EmpPW"), String)
                Employee1.Initials = CType(rdrEmp("EmpInit"), String).Trim
                EmployeeCollection1.Add(Employee1) 'I put watch here (on EmployeeCollection)
            Loop
            rdrEmp.Close()
        Catch eData As DataException
            MessageBox.Show(eData.Message, "ADO.NET error")
        Catch eSql As SqlException
            MessageBox.Show(eSql.Message, "SQL Server error")
        Finally
            SqlConnection2.Close()
        End Try
    End Sub

Public Class EmployeeCollection
    Inherits System.Collections.CollectionBase

    'add an employee
    Public Sub Add(ByVal newEmployee As Employee)
        List.Add(newEmployee)
    End Sub

    Public ReadOnly Property Item(ByVal index As Integer) As Employee
        Get
            Return CType(List.Item(index), Employee)
        End Get
    End Property
End Class
0
Comment
Question by:Turtleman05
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16705888

Private Sub LoadEmployees()
        Dim cmdEmp As New SqlCommand
        Dim i As Int16 = 0
        Dim obj As Object
        cmdEmp.Connection = SqlConnection2
        cmdEmp.CommandText = "SELECT EmpNum, EmpFName,EmpLName, EmpPW, EmpInit " _
            & " FROM Employee  ORDER BY EmpNum  "
        Try
            SqlConnection2.Open()
            Dim rdrEmp As SqlDataReader = cmdEmp.ExecuteReader()
            Do While rdrEmp.Read()
                Employee1 = new Employee()    <<< add this line
                Employee1.Number = CType(rdrEmp("EmpNum"), Integer)
                Employee1.FirstName = CType(rdrEmp("EmpFName"), String)
                Employee1.LastName = CType(rdrEmp("EmpLName"), String)
                Employee1.Password = CType(rdrEmp("EmpPW"), String)
                Employee1.Initials = CType(rdrEmp("EmpInit"), String).Trim
                EmployeeCollection1.Add(Employee1) 'I put watch here (on EmployeeCollection)
            Loop
            rdrEmp.Close()
        Catch eData As DataException
            MessageBox.Show(eData.Message, "ADO.NET error")
        Catch eSql As SqlException
            MessageBox.Show(eSql.Message, "SQL Server error")
        Finally
            SqlConnection2.Close()
        End Try
    End Sub

Public Class EmployeeCollection
    Inherits System.Collections.CollectionBase

    'add an employee
    Public Sub Add(ByVal newEmployee As Employee)
        List.Add(newEmployee)
    End Sub

    Public ReadOnly Property Item(ByVal index As Integer) As Employee
        Get
            Return CType(List.Item(index), Employee)
        End Get
    End Property
End Class
0
 

Author Comment

by:Turtleman05
ID: 16706165
Thankyou angelIII! Works great!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

826 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question