private void Save() { // required string sourceTable = SourceTables.SelectedValue; string destTable = DestTableName; int? destPriKey = null; Dictionary <string, Dictionary <int, string> > tablesRecordsAndKeys = new Dictionary <string, Dictionary <int, string> >(); tablesRecordsAndKeys.Add(sourceTable, new Dictionary <int, string>()); var recordsAndKeys = tablesRecordsAndKeys[sourceTable]; // determine if using actual records or eform if (IsActualRecord) { destPriKey = int.Parse(DestTablePrimaryKey); } foreach (RepeaterItem item in TableRecordsRptr.Items) { // get related record if (if it exists) HiddenField srcTableKeyField = item.FindControl("SrcTablePriKey") as HiddenField; HiddenField relatedRecordIdField = item.FindControl("RelatedRecordId") as HiddenField; // get dest key (should always exist) int srcPriKey = int.Parse(srcTableKeyField.Value); // get existing related record (if exists) int?relatedRecordId = null; if (!string.IsNullOrEmpty(relatedRecordIdField.Value)) { relatedRecordId = int.Parse(relatedRecordIdField.Value); } // iterate relation strengths and insert/update/delete relation (if applicable) bool doDelete = true; // mark inital strength for update/insert/delete, empty recordsAndKeys[srcPriKey] = string.Empty; foreach (int relationStrength in RelationStrengths) { RadioButton relationRadio = item.FindControl("Relation_Radio_" + relationStrength) as RadioButton; if (relationRadio.Checked) { // once an item is checked, no need to delete doDelete = false; // mark updated relation recordsAndKeys[srcPriKey] = relationStrength.ToString(); // if record exists (updated strength) if (relatedRecordId.HasValue) { RelatedRecord biz = new RelatedRecord(); biz.Get(relatedRecordId.Value); biz[RelatedRecord.RelationStrength] = relationStrength; biz.Save(); } // if there is a destination, update else if (destPriKey.HasValue) { RelatedRecord biz = RelatedRecordController.CreateRelatedRecord(RelatedRecordController.SOURCE_SYSTEM, sourceTable, srcPriKey, destTable, destPriKey.Value, relationStrength, true); } else if (IsEform) { // handled by update map } // only 1 checked radio per row break; } } // if no items have been checked, remove related record if (doDelete) { // delete real relation if (relatedRecordId.HasValue) { RelatedRecord biz = new RelatedRecord(); biz.Delete(relatedRecordId.Value); // update keys relatedRecordId = null; relatedRecordIdField.Value = string.Empty; } // delete eform reation else if (IsEform) { // handled by update map } } } // for eforms, insert/update/delete relationships if (IsEform) { // udpate xml XmlDocument eformXML = GetEformXml(); rc.UpdateEformRelatedRecords(eformXML, DestTableName, EformRecordId, tablesRecordsAndKeys); // update record EForm biz = new EForm(); biz.Get(int.Parse(EformId)); biz[EForm.EFormXML] = eformXML.OuterXml; biz.Save(); } // Register update script if (!string.IsNullOrEmpty(RelatedClientId))// && sourceTable == SrcTableName) { var relationStrengths = tablesRecordsAndKeys.SelectMany(a => a.Value.Select(b => b.Value)).Where(a => !string.IsNullOrEmpty(a)); string clientRelationArray = "[" + string.Join(",", relationStrengths.ToArray()) + "]"; Page.ClientScript.RegisterStartupScript(this.GetType(), "refreshAndCloseRelatedRecords", "refreshAndCloseRelatedRecords('" + RelatedClientId + "', " + clientRelationArray + ");", true); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "refreshAndCloseRelatedRecords", "refreshAndCloseRelatedRecords();", true); } }