public void SaveHSP(HighSchoolPolicyClass hsp, PolicyTextValidation pCheck) { string query = $"Update HighSchoolPolicy Set "; List <string> listToSave = new List <string>(new string[] { "parentSignature = @ParentSignature", "attendance = @Attendance", "tobacco = @Tobacco", "internetAccess = @InternetAccess", "studentInsurance = @StudentInsurance", "fieldTrips = @FieldTrips", "drugTesting = @DrugTesting", "noticeOfDisclosures = @NoticeOfDisclosures", "cellPhoneContact = @CellPhoneContact", "releaseForPhotography = @ReleaseForPhotography", "studentSignature = @signature" }); List <string> toRemove = pCheck.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 { ParentSignature = hsp.parentSignature, signature = hsp.studentSignature, Attendance = hsp.attendance, Tobacco = hsp.tobacco, InternetAccess = hsp.internetAccess, StudentInsurance = hsp.studentInsurance, FieldTrips = hsp.fieldTrips, DrugTesting = hsp.drugTesting, NoticeOfDisclosures = hsp.noticeOfDisclosures, CellPhoneContact = hsp.cellPhoneContact, ReleaseForPhotography = hsp.releaseForPhotography, id = hsp.Id }); } }