数据字段Null CRM 2015 C#

时间:2016-03-17 16:04:24

标签: c# dynamics-crm-2011 dynamics-crm crm dynamics-crm-2013

我的代码:

OrganizationServiceProxy service = OrganizationServiceProxy();
            QueryExpression qe = new QueryExpression();
            qe.EntityName = "account";
            qe.ColumnSet = new ColumnSet(new string[]{"accountid","name"});
            DataTable table=new DataTable();
            table.Columns.AddRange(new DataColumn[] { new DataColumn("accountid"), new DataColumn("name") });
            var Compte = service.RetrieveMultiple(qe).Entities.ToList();
            foreach (var item in Compte)
            {                    table.Rows.Add(item.Attributes["accountid"].ToString(), item.Attributes["name"].ToString());
            }
            comboBox1.DataSource = table;
            comboBox1.ValueMember = "accountid";
            comboBox1.DisplayMember = "name";

我的数据是这样的:

         +---------------------------------------+---------------+
                    accountid                          name
         +---------------------------------------+---------------+
            87906183-dbbb-4754-afc9-f2cfcab4942d    Lois Wright
            86a9c978-e1dc-40e2-98cd-6b463ded0f2d      Null
            8e242301-6c97-4509-9031-7237c1d7b14e    Wanda Torres
            2704b13c-8900-4216-98ce-6b03a056ed32      Null
            55f0db83-d4a7-426a-ac7a-a113118b9a1f    Howard Woods
         +---------------------------------------+---------------+

我收到错误“给定的密钥不在字典crm中”

1 个答案:

答案 0 :(得分:2)

您的示例中有两条记录对“name”字段具有“null”。查询CRM时,它只返回包含数据的属性。在这种情况下,您需要检查响应以确保它在尝试使用之前包含name属性:

.button {
  padding: 100px 15px;
  width: 150px;
  background-color: deepskyblue;
  color: white;
  margin: 3px;
  text-align: top;

  display: flex;
  flex-direction: column;
}

您还可以使用GetAttributeValue方法,如果该属性不存在而不是抛出异常,则返回null:

if (item.Attributes.Contains("name")) {...}

https://msdn.microsoft.com/en-us/library/gg326129.aspx