locked
List Object RRS feed

  • Question

  • User-1577735185 posted

    I'm pretty new to c# and building a simple api to read from a database.  So please forgive any massive amounts of id10t error.  I'm attempting to loop an sqlreader and write the results to a list.  I've consrtucted two data objects ServerAndRole and ServersAndRoles. When I read through the sql reader I can see using debug writeline that I am outputing the data i want but my final list of servers and roles ends up with all the same object in the list. so it looks like each loop is just rewriting the data.

    the constructor for server and role:

     public class ServerAndRole 
        {
            private Int32 i_ServerID;
            private Int32 i_RoleID;
            ....
           [DataMember]
            public Int32 ServerID
            {
                get { return i_ServerID; }
                set { i_ServerID = value; }
            }
            [DataMember]
            public Int32 RoleID
            {
                get { return i_RoleID; }
                set { i_RoleID = value; }
            }
            ....
    }
    
    

    For ServersAndRoles

     [KnownType(typeof(ServerAndRole))]
        public class ServersAndRoles : List<ServerAndRole>
        {
    
            private List<ServerAndRole> s_ServerAndRole;
            [DataMember]
            public List<ServerAndRole> ServerAndRole
            {
                get { return s_ServerAndRole; }
                set { s_ServerAndRole = value; }
            }
        }

    and the loop

     while (reader.Read())
                {
                    adServerAndRoleDResult.ServerID = reader.SafeGetInt32(reader.GetOrdinal("ServerID"));
                    adServerAndRoleDResult.RoleID = reader.SafeGetInt32(reader.GetOrdinal("RoleID"));
                    ....
                    Debug.WriteLine(adServerAndRoleDResult.ServerName);
                    adServersAndRolesDResult.Add(adServerAndRoleDResult);
                }

    The Debug.WriteLine is showing me each servername.

    My question: Am I messing up the adServersAndRolesDResult.Add(adServerAndRoleDResult); or the constructor for the ServersAndRoles object? If so what is the correct way to construct that object.  Thank you.

    Thursday, August 15, 2019 3:55 PM

Answers

  • User753101303 posted

    Hi,

    It seems you created an object before the loop and so you end up in updating and adding the same object to your list again an again. Instead you should create a new object inside the loop:

    while (reader.Read())
    {
      adServerAndRoleDResult=new ServerAndRole();
      adServerAndRoleDResult.ServerID = reader.SafeGetInt32(reader.GetOrdinal("ServerID"));
      adServerAndRoleDResult.RoleID = reader.SafeGetInt32(reader.GetOrdinal("RoleID"));
      ....
      Debug.WriteLine(adServerAndRoleDResult.ServerName);
      adServersAndRolesDResult.Add(adServerAndRoleDResult);
    }

    I would suggest also to have a look at Entity Framework that does this and more for you: https://docs.microsoft.com/en-us/ef/

    Also you can now use auto-implemented properties which simplifies the syntax when your property just read/write from a backend field :

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 15, 2019 4:13 PM

All replies

  • User753101303 posted

    Hi,

    It seems you created an object before the loop and so you end up in updating and adding the same object to your list again an again. Instead you should create a new object inside the loop:

    while (reader.Read())
    {
      adServerAndRoleDResult=new ServerAndRole();
      adServerAndRoleDResult.ServerID = reader.SafeGetInt32(reader.GetOrdinal("ServerID"));
      adServerAndRoleDResult.RoleID = reader.SafeGetInt32(reader.GetOrdinal("RoleID"));
      ....
      Debug.WriteLine(adServerAndRoleDResult.ServerName);
      adServersAndRolesDResult.Add(adServerAndRoleDResult);
    }

    I would suggest also to have a look at Entity Framework that does this and more for you: https://docs.microsoft.com/en-us/ef/

    Also you can now use auto-implemented properties which simplifies the syntax when your property just read/write from a backend field :

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 15, 2019 4:13 PM
  • User-1577735185 posted

    Boom thank you makes perfect sense.

    Thursday, August 15, 2019 4:19 PM