I'm trying to loop through items in a ASP c# Listbox, During the loop I intend to get the current value from the listbox and use it to fetch another value from the DB, then using the DB retrieved value to trigger another action (insert to dynamics CRM Database). I'm getting the error:
Collection was modified; enumeration operation may not execute.
Below is my code, any ideas what I am doing wrong?
foreach (object item in SelectedHobbies.Items)
{
string hobby = SelectedHobbies.Items.ToString();
string sql = "select cba_hobbyid from cba_hobby where cba_hobby = '" + hobby + "'";
string hobbyId = Global_Class.GetSqlValue2(sql, "cba_hobbyid");
try
{
using (OrganizationServiceProxy organizationServiceProxy = new OrganizationServiceProxy(this.OrganizationUri_prod, this.HomeRealmUri, this.Credentials, null))
{
IOrganizationService organizationService = organizationServiceProxy;
// Entity contact = new Entity("contact");
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference("contact", new Guid(hobbyId)));
Relationship relationship = new Relationship("vrp_cba_hobby_contact");
organizationService.Associate("contact", new Guid(contactId), relationship, relatedEntities);
}
}
catch (Exception exception1)
{
Exception exception = exception1;
}
}