private void CopyCandidates() { var candidatesCopied = 0; var officesCopied = 0; var officesSkipped = 0; try { //if (ElectionsPoliticians.CountByElectionKey(GetElectionKey()) > 0) // throw new VoteException( // "This function can only be used if no candidates have been added for the election."); var offices = ElectionsOffices.GetDataByElectionKey(GetElectionKey()); if (offices.Count == 0) { throw new VoteException( "There are no offices for this election to copy. Use the Add/Remove Offices tab to add the offices you want to copy."); } var electionKeyToCopy = MasterOnlyElectionKeyToCopy.Value; foreach (var office in offices) { if ( ElectionsPoliticians.CountByElectionKeyOfficeKey(GetElectionKey(), office.OfficeKey) > 0) { officesSkipped++; continue; } var oldPoliticians = ElectionsPoliticians.GetDataByElectionKeyOfficeKey(electionKeyToCopy, office.OfficeKey); if (oldPoliticians.Count == 0) { continue; } officesCopied++; candidatesCopied += oldPoliticians.Count; var newPoliticians = new ElectionsPoliticiansTable(); foreach (var oldPolitician in oldPoliticians) { newPoliticians.AddRow(office.ElectionKey, office.OfficeKey, oldPolitician.PoliticianKey, oldPolitician.RunningMateKey, office.ElectionKeyState, office.ElectionKeyFederal, office.ElectionKeyCounty, office.ElectionKeyLocal, office.StateCode, office.CountyCode, office.LocalCode, office.DistrictCode, oldPolitician.OrderOnBallot, false, OfficesOfficials.OfficeKeyPoliticianKeyExists(office.OfficeKey, oldPolitician.PoliticianKey), false); } ElectionsPoliticians.UpdateTable(newPoliticians); } FeedbackMasterOnly.AddInfo( officesCopied > 0 ? $"{candidatesCopied} candidates were copied for {officesCopied} offices." : "There were no candidates that could be copied."); if (officesSkipped > 0) { FeedbackMasterOnly.AddInfo( $"{officesSkipped} offices were skipped because there were already candidates entered."); } } catch (Exception ex) { FeedbackMasterOnly.PostValidationError(ControlMasterOnlyElectionToCopy, "The candidates could not be copied: " + ex.Message); } }