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)

Specified cast is not valid error

(0) ShareShare
ReportReport
Posted on by

Hello guys,
I have a piece of code which looks something like this :

QueryExpression queryname = new QueryExpression(entityname);
queryname.ColumnSet = new ColumnSet(fieldnames);
queryname.Criteria.AddCondition(new ConditionExpression(condition));
EntityCollection collectionName = _service.RetrieveMultiple(queryname);
if (collectionName.Entities.Count > 0)
{
tracingService.Trace("into method - 1, EC Count :" + collectionName.Entities.Count);
foreach (Entity entityname in productThresoldRecords.Entities)
//businesslogic..

I am getting an error in the foreach line - "Specified cast is not valid.".(Confirmed by the error log - the above trace is displayed . But the first trace in the foreach method is not..)

I can't figure out why I would get that error.Could it be because of the count of entity collection? Usually it is 1..

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi,

    Your result from the RetrieveMultiple is saved in a parameter called collectionName, not productThresholdRecords.

    Try replace that and see if it works.

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hi Aric,

    Thanks for the response. Sorry I forgot to replace the sample name for that. It is the parameter name from Entity Collection. So it looks like this only :

    QueryExpression queryname = new QueryExpression(entityname);

    queryname.ColumnSet = new ColumnSet(fieldnames);

    queryname.Criteria.AddCondition(new ConditionExpression(condition));

    EntityCollection collectionName = _service.RetrieveMultiple(queryname);

    if (collectionName.Entities.Count > 0)

    {

    tracingService.Trace("into method - 1, EC Count :" + collectionName.Entities.Count);

    foreach (Entity entityname in collectionName.Entities)

    //businesslogic..

    Still getting the error.

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Not sure if you are replacing your code for the forum, but you also have entityname appear once in the name of the Query Expression and once in the foreach statement.

    Basically the following should work:

    QueryExpression query = new QueryExpression(entityName);

    query.ColumnSet = new ColumnSet(fieldNames);

    query.Criteria.AddCondition(new ConditionExpression(condition));

    EntityCollection results = _service.RetrieveMultiple(queryname);

    if (results.Entities.Count > 0)

    {

      tracingService.Trace("into method - 1, EC Count :" + results.Entities.Count);

      foreach (Entity current in results.Entities)

      {

      }

    }

    I would basically check syntax, since this is pretty straight forward.

    Are you sure the error is in the foreach statement?

  • Community Member Profile Picture
    on at

    Aric,

    Apologies for the confusion. Yes - my code looks like how you have mentioned. Yes, the error is in the foreach statement. I confirmed it by inserting a trace message before and after the foreach. Could it be because of the data am retrieving?

    My condition expression checks if a lookup field is equal to a guid. I am also retrieving some lookup fields. However, the queryexpression is retrieving something. Because in the trace, the count is 1 - expected behaviour.

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    So, do you have entityname appear twice (once in the query expression and once in the for each statement)?

  • Community Member Profile Picture
    on at

    No just once . My code looks like how you mentioned :

    QueryExpression query = new QueryExpression(entityName);

    query.ColumnSet = new ColumnSet(fieldNames);

    query.Criteria.AddCondition(new ConditionExpression(condition));

    EntityCollection results = _service.RetrieveMultiple(query);

    if (results.Entities.Count > 0)

    {

     tracingService.Trace("into method - 1, EC Count :" + results.Entities.Count);

     foreach (Entity current in results.Entities)

     {

     }

    }

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    Hi,

     what's the first trace in the foreach? What are you tracing there? I think the cast error is, actually, in that line..

  • Verified answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Can you show what code you have inside the foreach, and do have have any tracing there that is displayed?

  • Community Member Profile Picture
    on at

    Aric and Alex,

    You guys were right.  The error was due to the trace immediately into the foreach method.  I was treating a decimal variable as int. Fixed the issue

    Thank you!

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