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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Error when retrieve an empty money data type

(0) ShareShare
ReportReport
Posted on by

I keep getting an error says "The given key was not present in the dictionary" when trying to retrieve a record with empty money field. 

decimal estRevBase = ((Money)cloudent["estimatedvalue"]).Value;

Above code is succes when the money field is not empty. But when money field is empty, 
it's returning an error.

I tried using 

decimal ?

and Convert the decimal to string, to check wether the estrevbase is null or not.

decimal estRevBase = ((Money)cloudent["estimatedvalue"]).Value;
string des = estRevBase.ToString(); 
if (!string.IsNullOrEmpty(des))
    {
      estRev = Convert.ToDecimal(des);
    }
    else
    {
     estRev = 0 ;
    }

Still no luck :(

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at
    RE: Error when retrieve an empty money data type
    Please use a check before assigning values :

    if (cloudent.Attributes.Contains("estimatedvalue")){
    decimal estRevBase = ((Money)cloudent["estimatedvalue"]).Value;
    }
    

  • Community Member Profile Picture
    on at
    RE: Error when retrieve an empty money data type

    Thanks

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at
    RE: Error when retrieve an empty money data type

    Hi,

    Please try the following code.

    var estRevBase = (Money)cloudent["estimatedvalue"];
    if(estRevBase !=null)
    {
       decimal estRevBaseValue = estRevBase.Value;
    } 

    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

Responsible AI policies

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

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans