web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Error: 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll while retrieving the notes in crm2011

(0) ShareShare
ReportReport
Posted on by

How to check whether Annotation text contains data or not, I am using below code to retrieve multiple annotations, if the note text contains data I am not getting any error, if text is null i am getting this error :: A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll

Below is my code.

public static void RetrieveMultiple(IOrganizationService _service)
{

ColumnSet objColumns = new ColumnSet();


objColumns.AddColumns(new string[] { "annotationid", "createdby","notetext","subject"});


ConditionExpression objConditionExpression1 = new ConditionExpression("subject", ConditionOperator.NotNull);


ConditionExpression objConditionExpression2 = new ConditionExpression("notetext", ConditionOperator.NotNull);


//FilterExpression filterPrincipal = new FilterExpression();


// filterPrincipal.FilterOperator = LogicalOperator.And;
//filterPrincipal.AddCondition(objConditionExpression1);
// filterPrincipal.AddCondition(objConditionExpression2);

//Create the QueryExpression.
QueryExpression queryPrincipal = new QueryExpression();

// Set the properties of the QueryExpression.
queryPrincipal.EntityName = "annotation";
queryPrincipal.ColumnSet = objColumns; //queryPrincipal.ColumnSet = new Columnset(true);
//queryPrincipal.Criteria = filterPrincipal;


EntityCollection ec = _service.RetrieveMultiple(queryPrincipal);
Console.WriteLine("Count of {0} Retrived", ec.Entities.Count);
foreach (Entity ent in ec.Entities)
{
if(ent["subject"] != null)
{
string Sub = ent["subject"].ToString();
Console.WriteLine("Subject:" + Sub);
}
string Text = ent["notetext"].ToString();
if (Text != null)
{

Console.WriteLine("Notes:" + Text);
}
if (ent["createdby"] != null)
{
string created = ((EntityReference)ent["createdby"]).Name;
Console.WriteLine("Createdby:" + created);
}
if (ent["annotationid"] != null)
{
string id = ent["annotationid"].ToString();
Console.WriteLine("GUID:" + id);
}






}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Saroj Das Profile Picture
    3,355 on at

    hi Chaitanya, have a look into this

    stackoverflow.com/.../crm-2011-keynotfoundexception-exception

    Thanks

  • NKC Profile Picture
    on at

    Hi

    Thanks for this it worked, could you explain what does this means

    The error means that particular field is not present in the collection of properties. In CRM, only properties that have been set or updated are included.

  • Verified answer
    Royal King Profile Picture
    27,686 on at

    Use the below code to avoid error when note text is null .

    var note = ent.GetAttributeValue<string>("notetext");

    or

    if(ent.Contains("notetext"))

    {

    var note = ent["ent"];

    }

  • NKC Profile Picture
    on at

    Ah ... totally forgot, i used this previously in plugins. Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans