public List<Member> GetAllMembers() { List<Member> memberListToReturn = new List<Member>(); Member memObj = null; string connString = ConfigurationManager.ConnectionStrings["CS1"].ConnectionString; using (SqlConnection con = new SqlConnection(connString)) { string selectCmd = "SELECT * FROM Member"; SqlCommand cmd = new SqlCommand(selectCmd, con); cmd.CommandType = CommandType.Text; try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { if ((Member.MemberType)reader["membertype"] == Member.MemberType.GoldMember) { memObj = new GoldMember() { Firstname = reader["firstname"].ToString(), Lastname = reader["lastname"].ToString(), Address = reader["address"].ToString(), City = reader["city"].ToString(), Phonenum = reader["phonenumber"].ToString(), Email = reader["email"].ToString(), FacilityName = reader["facilityname"].ToString(), DietistName = reader["dietistname"].ToString(), Type = Member.MemberType.GoldMember }; memberListToReturn.Add(memObj); } else { memObj = new SilverMember() { Firstname = reader["firstname"].ToString(), Lastname = reader["lastname"].ToString(), Address = reader["address"].ToString(), City = reader["city"].ToString(), Phonenum = reader["phonenumber"].ToString(), Email = reader["email"].ToString(), InstructorName = reader["instructorname"].ToString(), Type = Member.MemberType.SilverMember }; memberListToReturn.Add(memObj); } } } catch (Exception ex) { throw; } } return memberListToReturn; }
public Member FindMember(int ID) { Member memberObjToReturn = null; string connString = ConfigurationManager.ConnectionStrings["CS1"].ConnectionString; using (SqlConnection con = new SqlConnection(connString)) { string selectCmd = "SELECT * FROM Member WHERE Id=@ID"; SqlCommand cmd = new SqlCommand(selectCmd, con); cmd.CommandType = CommandType.Text; SqlParameter parameterID = new SqlParameter(); parameterID.ParameterName = "@ID"; parameterID.Value = ID; cmd.Parameters.Add(parameterID); con.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { if ((Member.MemberType)reader["membertype"] == Member.MemberType.GoldMember) { memberObjToReturn = new GoldMember() { MemberID = Convert.ToInt32( reader["Id"]), Firstname = reader["firstname"].ToString(), Lastname = reader["lastname"].ToString(), Address = reader["address"].ToString(), City = reader["city"].ToString(), Phonenum = reader["phonenumber"].ToString(), Email = reader["email"].ToString(), FacilityName = reader["facilityname"].ToString(), DietistName = reader["dietistname"].ToString(), Type = Member.MemberType.GoldMember }; } else { memberObjToReturn = new SilverMember() { MemberID = Convert.ToInt32(reader["Id"]), Firstname = reader["firstname"].ToString(), Lastname = reader["lastname"].ToString(), Address = reader["address"].ToString(), City = reader["city"].ToString(), Phonenum = reader["phonenumber"].ToString(), Email = reader["email"].ToString(), InstructorName = reader["instructorname"].ToString(), Type = Member.MemberType.SilverMember }; } } } } return memberObjToReturn; }