public void SaveHSBI(HighSchoolBasicInfoClass hsbi, HighSchoolBasicInfoTextValidation hsbiCheck) { string query = $"Update HighSchoolBasicInfo Set "; List <string> listToSave = new List <string>(new string[] { "lastName = @LastName", "firstName = @FirstName", "middleInitial = @MiddleInitial", "preferredName = @PreferredName", "program = @Program", "streetAddress = @StreetAddress", "city = @City", "state = @State", "zipCode = @ZipCode", "primaryPhoneNum = @PrimaryPhoneNum", "cellPhoneNum = @CellPhoneNum", "hispanicOrLatino = @HispanicOrLatino", "race = @Race", "gender = @Gender", "dateOfBirth = @DateOfBirth", "currentEdLevel = @CurrentEdLevel", "sendingHS = @SendingHS" }); List <string> toRemove = hsbiCheck.IsValid; foreach (var v in toRemove) { for (int i = 0; i < listToSave.Count; i++) { if (listToSave[i].Contains(v)) { listToSave.RemoveAt(i); i--; } } } foreach (var s in listToSave) { if (listToSave.IndexOf(s) != listToSave.Count - 1) { query += s + ","; } else { query += s; } } query += " Where Id = @id"; using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("EnrollmentDB"))) { connection.Execute(query, new { LastName = hsbi.lastName, FirstName = hsbi.firstName, MiddleInitial = hsbi.middleInitial, PreferredName = hsbi.preferredName, Program = hsbi.program, StreetAddress = hsbi.streetAddress, City = hsbi.city, State = hsbi.state, ZipCode = hsbi.zipCode, PrimaryPhoneNum = hsbi.primaryPhoneNum, CellPhoneNum = hsbi.cellPhoneNum, HispanicOrLatino = hsbi.hispanicOrLatino, Race = hsbi.race, Gender = hsbi.gender, DateOfBirth = hsbi.dateOfBirth, CurrentEdLevel = hsbi.currentEdLevel, SendingHS = hsbi.sendingHS, id = hsbi.Id }); } }