Groups | Blog | Home
all groups > dotnet ado.net > march 2008 >

dotnet ado.net : Loading a generic collection from a SqlReader?


Dave
3/27/2008 1:35:01 PM
Hi, is there any underlying difference between loading a generic collection
from a reader using the two methods:

List<User> colUsers = new List<Users>();

while (reader.Read())
{
User user = new User();
user.FirstName = (string)reader["FirstName"];
user.LastName = (string)reader["LastName"];
colUsers .Add(user);
}

----vs----

while (reader.Read())
{
colUsers.Add(new User
(
(string)reader["FirstName"],
(string)reader["LastName"]
));
}


Creating the object first and then adding values worked fine. But using the
second method by loading the values in the constructor, the colUsers.Count
was correct based on the query, but each of the object's properties in the
collection were null. it's as if the object's weren't retaining their values
Dave
3/28/2008 6:09:01 AM
nevermind...

[quoted text, click to view]
AddThis Social Bookmark Button