locked
How to convert a string type to a money type or how to typecast a string type to money type in C# in crm2011? RRS feed

  • Question

  • Hi

    I want to convert a string type to money type in crm2011.Is it possible? Is there any alternative way to handle this?

    I am using the following piece of code and it is throwing error.It says cannot convert string type to money type..

    mny =((CrmSdk.Money)objCol.txt1).Value;

    Thanks..


    Dibyasingh Tripathy

    Tuesday, February 21, 2012 5:26 AM

Answers

  • Hi,

    use decimal to convert

                        string stringmoney = "1234.56";
                        retrievedAccount.Revenue = new Money(decimal.Parse(stringmoney));
    Money mny = new Money(decimal.Parse(objCol.txt1));



    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


    Tuesday, February 21, 2012 5:49 AM
  • Hi thomas,

    Finally I got the answer so your answer helped me a lot..:)

    The correct way is..

     CrmSdk.Money mny = new Money(){Value=decimal.Parse(objCol.txt1)};

    More similar to your answer...:)

    Thank you..


    Dibyasingh Tripathy

    Tuesday, February 21, 2012 7:25 AM

All replies

  • Hi,

    use decimal to convert

                        string stringmoney = "1234.56";
                        retrievedAccount.Revenue = new Money(decimal.Parse(stringmoney));
    Money mny = new Money(decimal.Parse(objCol.txt1));



    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


    Tuesday, February 21, 2012 5:49 AM
  • Hi Thomas

    I tried what you wrote but it giving the exception "Cannot implicitly convert decimal to type money" in both the below cases

    Money mny=new Money();

     mny =(CrmSdk.Money) decimal.Parse(objCol.txt1);

    mny=decimal.Parse(objCol.txt1);

    Thank you for your response..

    Can you give me some other idea?

    Thanks


    Dibyasingh Tripathy


    Tuesday, February 21, 2012 6:14 AM
  • try the second way 

    Money mny = new Money(decimal.Parse(objCol.txt1));


    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Tuesday, February 21, 2012 6:19 AM
  • Hi Thomas,

    This time I am getting the error which says "Money doesnot contain a constructor which take one arguement."

    Money mny = new Money(decimal.Parse(objCol.txt1));

    Thanks


    Dibyasingh Tripathy

    Tuesday, February 21, 2012 6:30 AM
  • I am not sure why you are getting the error, i tested with VS2010, and it is not giving me any error

    examples as follows:

                        string stringmoney = "1234.56";
                        retrievedAccount.Revenue = new Money(decimal.Parse(stringmoney));

                        Money mny = new Money(decimal.Parse(stringmoney));

    see the following from SDK

    Money () <summary xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">Initializes a new instance of theMoney class.</summary>
    Money (Decimal)

    <summary xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">Initializes a new instance of theMoney class setting the value.</summary>

    <summary xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5"></summary>

    may be in your code, you need to specify CrmSdk.Money 


    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Tuesday, February 21, 2012 6:37 AM
  • Hi thomas

    I don't have any idea either..Well I tried your code and got the following error

     string strMoney = "1234.56";
     CrmSdk.Money mny = new Money(decimal.Parse(strMoney));

    I am really surprised how is it working at your end not mine..I am using VS2010 as well..

    Any idea what may be the reason?

    Anyways I don't know what is that SDk you are refering to as I am a new comer..So I don't know something might be missing in my code.

    CrmSdk is the SOAP Service Reference that I have used in my code..

    Thank you again


    Dibyasingh Tripathy


    Tuesday, February 21, 2012 6:49 AM
  • It looks like you are using CRM 4 web reference endpoints from CRM 2011.

    The SDK I am referning is, you can download CRM 2011 SDK (Microsoft Dynamics CRM 2011 Software Development Kit (SDK)) from MS site, which include help file, and reference assemblies which you can use for such as microsoft.xrm.sdk.dll ..etc, the latest version is 5.0.9

    you can download it from here:  http://www.microsoft.com/download/en/details.aspx?id=24004

    Please download and look at the help file crmsdk2011.chm which has heaps of information.


    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Tuesday, February 21, 2012 7:00 AM
  • Hi Thomas

    Well I am using CRM2011 live and the SOAP endpoints of the same..And I am working with silverlight web resource..And when I am trying to add a reference to the microsoft.xrm.sdk.dll it says

    "You cannot add a reference to microsoft.xrm.sdk.dll as it was not built against the silverlight runtime.Silverlight projects will only work with Silverlight assmblies"

    So What can I do?

    Is there any idea?

    Thank you once again..


    Dibyasingh Tripathy

    Tuesday, February 21, 2012 7:08 AM
  • Hi thomas,

    Finally I got the answer so your answer helped me a lot..:)

    The correct way is..

     CrmSdk.Money mny = new Money(){Value=decimal.Parse(objCol.txt1)};

    More similar to your answer...:)

    Thank you..


    Dibyasingh Tripathy

    Tuesday, February 21, 2012 7:25 AM