【发布时间】:2019-12-04 10:15:55
【问题描述】:
我正在尝试遍历 ASP c# 列表框中的项目,在循环期间我打算从列表框中获取当前值并使用它从数据库中获取另一个值,然后使用数据库检索到的值来触发另一个操作(插入动态 CRM 数据库)。我收到错误:
集合已修改;枚举操作可能无法执行。
下面是我的代码,你知道我做错了什么吗?
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;
}
}
【问题讨论】:
-
您好!欢迎来到 SO。这能回答你的问题吗?stackoverflow.com/questions/1538817/…
-
除了原始问题之外,我还发现了一些其他问题并希望解决,但是,此代码是
foreach块中的所有代码还是您省略了总和。因为我看不到错误可能出现在哪里 -
这是'字符串爱好 = SelectedHobbies.Items.ToString();'正确还是应该是'string hobby = item.ToString();'?我不确定这是否与我问的错误有关。
-
实际上,如果您修改
SelectedHobbies.Items,则会出现此错误,但在您发布的代码中,我没有看到任何明确的指示。另一种可能性是您更改了底层数据绑定数据集合。您能否将代码发布到您用项目填充列表框的位置? -
方法:
Associate是做什么的?