/// <summary> /// Handles the Click event of the btnFindByEnvelopeNumber control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void btnFindByEnvelopeNumber_Click(object sender, EventArgs e) { var rockContext = new RockContext(); var personGivingEnvelopeAttribute = AttributeCache.Read(Rock.SystemGuid.Attribute.PERSON_GIVING_ENVELOPE_NUMBER.AsGuid()); var envelopeNumber = tbEnvelopeNumber.Text; if (!string.IsNullOrEmpty(envelopeNumber)) { var personIdsWithEnvelopeNumber = new AttributeValueService(rockContext).Queryable() .Where(a => a.AttributeId == personGivingEnvelopeAttribute.Id && a.Value == envelopeNumber) .Select(a => a.EntityId.Value); var count = personIdsWithEnvelopeNumber.Count(); var personService = new PersonService(rockContext); if (count == 0) { lEnvelopeSearchResults.Text = string.Format("No individual found with envelope number of {0}.", envelopeNumber); cblEnvelopeSearchPersons.Visible = false; mdEnvelopeSearchResults.SaveButtonText = string.Empty; mdEnvelopeSearchResults.Show(); } else if (count == 1) { var personId = personIdsWithEnvelopeNumber.First(); ppSelectNew.SetValue(personService.Get(personId)); LoadPersonPreview(personId); } else { lEnvelopeSearchResults.Text = string.Format("More than one person is assigned envelope number {0}. Please select the individual you wish to use.", envelopeNumber); cblEnvelopeSearchPersons.Visible = true; cblEnvelopeSearchPersons.Items.Clear(); var personList = personService.Queryable().Where(a => personIdsWithEnvelopeNumber.Contains(a.Id)).AsNoTracking().ToList(); foreach (var person in personList) { cblEnvelopeSearchPersons.Items.Add(new ListItem(person.FullName, person.Id.ToString())); } mdEnvelopeSearchResults.SaveButtonText = "Select"; mdEnvelopeSearchResults.Show(); } } }