Click here to Skip to main content
13,046,045 members (45,982 online)
Rate this:
Please Sign up or sign in to vote.
See more: (untagged)
I have the following method. In the While block i add different objects to the list, but when the list is returned all its elements are the same. Can help me?

public static List<Bo_Session> ReadAll(DateTime date)
SqlConnection connection = GetConnection();
string procedureName = "[dbo].[sp_UserSession_SelectAll]";
SqlCommand selectCommand = new SqlCommand(procedureName, connection);
selectCommand.CommandType = CommandType.StoredProcedure;
selectCommand.Parameters.AddWithValue("@pI_Date", date);
List<Bo_Session> bo_Session_List = new List<Bo_Session>();
SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.Default);

while (reader.Read())
Bo_Session locale_bo_Session = new Bo_Session();

locale_bo_Session.SessionId = System.Convert.ToInt32(reader["SessionId"] is DBNull ? 0 : reader["SessionId"]);
locale_bo_Session.LoggedUserId = System.Convert.ToInt32(reader["UserId"] is DBNull ? 0 : reader["UserId"]);
locale_bo_Session.LoggedUserName = System.Convert.ToString(reader["UserName"] is DBNull ? "" : reader["UserName"]);
locale_bo_Session.ApplicationId = System.Convert.ToInt32(reader["ApplicationId"] is DBNull ? 0 : reader["ApplicationId"]);
locale_bo_Session.ComputerName = System.Convert.ToString(reader["ComputerName"] is DBNull ? "" : reader["ComputerName"]);
locale_bo_Session.SessionStart = System.Convert.ToDateTime(reader["SessionStart"] is DBNull ? System.DateTime.Now : reader["SessionStart"]);
locale_bo_Session.SessionEnd = System.Convert.ToDateTime(reader["SessionEnd"] is DBNull ? System.DateTime.Now : reader["SessionEnd"]);
locale_bo_Session.SessionLastRetrieve = System.Convert.ToDateTime(reader["SessionLastRetrieve"] is DBNull ? System.DateTime.Now : reader["SessionLastRetrieve"]);
locale_bo_Session.SessionStatus = System.Convert.ToBoolean(reader["SessionStatus"] is DBNull ? 0 : reader["SessionStatus"]);


catch (SqlException ex)
throw ex;
return bo_Session_List;
Posted 6-Jul-10 12:58pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This looks correct to me, you create a new object inside the loop, so I see no reason for it to fail. Have you stepped through the code to see what is happening ?
Ale2010 6-Jul-10 20:07pm
Yes, I debugged it and when add a new object to the list, all the previous objects take the same values. It's really strange...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 6 Jul 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100