Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Specified cast is not valid error

Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Specified cast is not valid error

    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!

  • Verified answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Specified cast is not valid error

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

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Specified cast is not valid error

    Hi,

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Specified cast is not valid error

    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)

     {

     }

    }

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Specified cast is not valid error

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Specified cast is not valid error

    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 Profile Picture
    Aric Levin 30,188 on at
    RE: Specified cast is not valid error

    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
    Community Member Microsoft Employee on at
    RE: Specified cast is not valid error

    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.

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Specified cast is not valid error

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans