This code gives me two records:
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["simsConnectionString"].ConnectionString); cn.Open(); SqlCommand cmd = new SqlCommand("SELECT c.ID, c.ClassName FROM Class AS c INNER JOIN TeacherClass as t ON c.ID = t.ClassID WHERE t.TeacherID = " + intTeacherID, cn); SqlDataReader dr = cmd.ExecuteReader();
which are:
# | ID | Name |
1 | 52 | Form 1 A |
2 | 54 | Form 1 C |
Then I assign the values from the SQLDataReader to a List<classes>
Code for the Class classes:
public class classes { public int id = 0; public string name = ""; }
Code for the assignment:
List<classes> cls = new List<classes>(); classes classdata = new classes(); while (dr.Read()) { classdata.id = 0; classdata.name = null; classdata.id = Convert.ToInt32( dr[0].ToString()); classdata.name = dr[1].ToString(); cls.Add(classdata); }
I end up with two instances of ID=54 and name = Form 1 C in cls. Why?
Please help.