Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Object to Int Cast

(0) ShareShare
ReportReport
Posted on by 85

Hi Experts,

I am writing a plugin. I am trying to assign the value retrieved after running the query to variable of type int. But I am getting an error message : System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.OptionSetValue' to type 'System.IConvertible'.
at System.Convert.ToInt32(Object value) .

Below is my code:

int serviceProviderCountry = 0;

foreach (var v in serviceProDetails.Entities)
{
debugString += "\nSapId Found " +sapId;
serviceProviderCountry = Convert.ToInt32(v.Attributes["new_address1country"]);

Please suggest the correct syntax to for this conversion.

Thanks 

Amrita

*This post is locked for comments

  • Amrita P Profile Picture
    85 on at
    RE: Object to Int Cast

    Hi Guido,

    Thanks for the help. It is working.

    Thanks

    Amrita

  • Verified answer
    Kalpavruksh D365 CoE Profile Picture
    2,545 on at
    RE: Object to Int Cast

    Hi Amrita,

    Above answer is correct. However, you can also try the following: 

    foreach(var v in serviceProDetails.Entities)
    {
    debugString += "\nSapId Found " +sapId;
    if(v.contains("new_address1country") && v["new_address1country"] != null)
    {
    serviceProviderCountry = ((OptionSetValue)v.Attributes["new_address1country"]).Value;
    }
    }
    

    Please mark this answer as verified, if it helped you.

  • Verified answer
    Guido Preite Profile Picture
    54,073 Moderator on at
    RE: Object to Int Cast

    you will need to get first the v value

    OptionSetValue optAddressCountry = v.GetAttributeValue<OptionSetValue>("new_address1country");

    and after extract the int value

    if (optAddressCountry != null) {
        serviceProviderCountry = optAddressCountry.Value;
    }


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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,850 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans