2

I'm trying to update the invoice field "freightamount" from my C# code. I'm able to update other fields well like strings and Guid but for Money I get this error on creating or updating the invoice entity:

There was an error while trying to serialize parameter      
 http://schemas.microsoft.com/xrm/2011/Contracts/Services:entity. The InnerException 
 message was 'Type 'Microsoft.Xrm.Sdk.Money' with data contract name 
'Money:http://schemas.microsoft.com/xrm/2011/Contracts' is not expected. Consider using a 
DataContractResolver or add any types not known statically to the list of known types - 
for example, by using the KnownTypeAttribute attribute or by adding them to the list of 
 known types passed to DataContractSerializer.'.  Please see InnerException for more 
 details.

Here's a part of my code:

I use the reference to "Microsoft.Xrm.Sdk"

Then in code:

 invoice.Attributes.Add(new KeyValuePair<string, object>("freightamount",  new Microsoft.Xrm.Sdk.Money (row.amount)));

Where row.amount is a decimal value.

user3340627
  • 3,023
  • 6
  • 35
  • 80
  • can you try: `invoice["freightamount"] = new Money(row.amount);` – Guido Preite Jun 17 '14 at 10:40
  • I get the error : "Cannot apply indexing to an expression of type MyProjectName.CRMServiceName.Entity " – user3340627 Jun 17 '14 at 11:12
  • Are you using early bound entity types? If so, didn't you forget to call EnableProxyTypes() on the OrganizationServiceProxy object? – Henk van Boeijen Jun 17 '14 at 21:13
  • No, I'm using LateBound. invoice is of type "Entity". I am able to update any field other than "Currency" fields. I had to create a new field of type "Decimal" to add the invoice amount. – user3340627 Jun 18 '14 at 09:56
  • When you do a select using latebound, what value type is amount? I'm guessing Money is only used for earlybound, and you should be passing in a decimal instead of money. – Daryl Jun 18 '14 at 13:05
  • I tried it, I still get "Invalid Cast Exception" – user3340627 Jun 19 '14 at 07:33

1 Answers1

1

Have you tried adding Money as a KnownTypeAttribute for the Entity class by adding it to the web.config?

<system.runtime.serialization>
    <dataContractSerializer>
        <declaredTypes>
            <add type="Microsoft.Xrm.Sdk.Entity, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
                <knownType type = "Microsoft.Xrm.Sdk.Money, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>                 
             </add>
        </declaredTypes>
    </dataContractSerializer>
</system.runtime.serialization>
kalthir
  • 797
  • 7
  • 14