I have a method that queries CRM 2011 and then binds the results to a DropDownList, this is my code:
public void CheckRsmPopulateAccounts()
{
string rsmFirstName = _currentUser.FirstName;
string rsmLastName = _currentUser.LastName;
ddlCustomer.DataSource = (from r in gServiceContext.CreateQuery("opportunity")
join c in gServiceContext.CreateQuery("account") on ((EntityReference)r["accountid"]).Id equals c["accountid"]
join u in gServiceContext.CreateQuery("systemuser") on ((EntityReference)r["ownerid"]).Id equals u["systemuserid"]
where u["firstname"].Equals(rsmFirstName) && u["lastname"].Equals(rsmLastName)
select new
{
AccountId = !r.Contains("accountid") ? string.Empty : r["accountid"],
Account = !c.Contains("name") ? string.Empty : r["name"]
});
ddlCustomer.DataValueField = "AccountId";
ddlCustomer.DataTextField = "Account";
ddlCustomer.DataBind();
}
But for some reason it keeps giving me a Exception has been thrown by the target of an invocation. error, on it looks like the DataBind command. I can’t seem to figure out the issue, the error seems so generic. Any idea what is causing this?
Thanks!
select new {}creates an anonymous type. You cannot enumerate through an anonymous typed object.add
.ToList()at the end of your linqcheck also that your Linq is actually returning something and make sure that you don’t have any columns that comes back
null