Turtleman05
asked on
All items in collection same as the last iteration.
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(Em ployee1) 'I put watch here (on EmployeeCollection)
Loop
rdrEmp.Close()
Catch eData As DataException
MessageBox.Show(eData.Mess age, "ADO.NET error")
Catch eSql As SqlException
MessageBox.Show(eSql.Messa ge, "SQL Server error")
Finally
SqlConnection2.Close()
End Try
End Sub
Public Class EmployeeCollection
Inherits System.Collections.Collect ionBase
'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
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(Em
Loop
rdrEmp.Close()
Catch eData As DataException
MessageBox.Show(eData.Mess
Catch eSql As SqlException
MessageBox.Show(eSql.Messa
Finally
SqlConnection2.Close()
End Try
End Sub
Public Class EmployeeCollection
Inherits System.Collections.Collect
'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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER