Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: list
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>();
try
{
connection.Open();
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"]);
 
bo_Session_List.Add(locale_bo_Session);
 
}
reader.Close();
}
catch (SqlException ex)
{
throw ex;
}
finally
{
connection.Close();
}
return bo_Session_List;
}
Posted 6-Jul-10 13:58pm
Ale2010359

1 solution

Rate this: bad
good
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 ?
  Permalink  
Comments
Ale2010 at 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
0 OriginalGriff 290
1 Jochen Arndt 165
2 DamithSL 125
3 PIEBALDconsult 110
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 6 Jul 2010
Copyright © CodeProject, 1999-2014
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