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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Cannot implicitly convert string to Money

(0) ShareShare
ReportReport
Posted on by

Hi,

public class Quote
{
public Guid Id { get; set; }
public string LineNumber { get; set; }
public string ItemCode { get; set; }
public string ItemDesc { get; set; }
public string Quantity { get; set; }
public string No_of_Applications { get; set; }
public string Unit { get; set; }
public string Discount { get; set; }
public string UnitPrice { get; set; }
public string NetPrice { get; set; }

}


foreach (var data in retrv.Entities) { lstData.Add( new Quote() { Id = data.Id, LineNumber = data.Attributes.Contains("new_name") ? data["new_name"].ToString() : string.Empty, ItemCode = data.Attributes.Contains("new_itemcode") ? data["new_itemcode"].ToString() : string.Empty, ItemDesc = data.Attributes.Contains("new_itemdescription") ? data["new_itemdescription"].ToString() : string.Empty, Quantity = data.Attributes.Contains("new_quantity") ? data["new_quantity"].ToString() : string.Empty, No_of_Applications = data.Attributes.Contains("new_numberofapplications") ? data["new_numberofapplications"].ToString() : string.Empty, Unit = data.Attributes.Contains("new_unit") ? data["new_unit"].ToString() : string.Empty, Discount = data.Attributes.Contains("new_discount") ? data["new_discount"].ToString() : string.Empty, UnitPrice =data.Attributes.Contains("new_unitprice") ? data["new_unitprice"].ToString() : string.Empty , NetPrice = data.Attributes.Contains("new_netprice") ? data["new_netprice"].ToString() : string.Empty } ); }


I am getting below error for the above code;

Error: Cannot implicitly convert string to Money

and it prints Microsoft.Xrm.Sdk.Money at the runtime when i use string return type instead of money in Quote Class.

Thanks

please help me asap.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    refer to the article below:

    msdn.microsoft.com/.../0c899ak8.aspx

    Hope this help!

  • Habeeb-Dynamics CRM Application Developer Profile Picture
    on at

    please try to give me with syntax or example

  • Suggested answer
    Community Member Profile Picture
    on at

    Ok, Here is a simple example:

    Microsoft.Xrm.Sdk.Money valueMoney = new Microsoft.Xrm.Sdk.Money(100);

               string valueStr = valueMoney.ToString(); //this return "Microsoft.Xrm.Sdk.Money"

               string valueStr2 = valueMoney.Value.ToString(); // this return "100"

    Hope this help!

    If you're satisfied with my answer, please mark it as verified.

  • Verified answer
    Community Member Profile Picture
    on at

    For your code, try this:

    NetPrice = data.Attributes.Contains("new_netprice") ? data.GetAttributeValue<Money>("new_netprice").Value.ToString() : string.Empty

  • Verified answer
    Bhuvaneswary Rajasekaran Profile Picture
    405 on at

    Money amount1 = new Money(25);
    Money amount2 = new Money(30);

    Console.WriteLine("Amount 1 " + amount1.Value.ToString());
    Console.WriteLine("Amount 2 " + amount2.Value.ToString());

    Output:

    Amount 1 25

    Amount 2 30

  • Verified answer
    ManirajKV Profile Picture
    1,477 on at

    Hi,

    try this

    if your return type is string, follow this

    UnitPrice =data.Attributes.Contains("new_unitprice") ? ((Money)data["new_unitprice"]).Value.ToString() : string.Empty 

    if your return type is decimal((Money)data["new_unitprice"]).Value

  • Habeeb-Dynamics CRM Application Developer Profile Picture
    on at
                string id = guidBox.Text;
                Guid guid = Guid.Parse(id);
                string itemcode =itmcodeBox.Text;
                string itemdesc = itmdescBox.Text;
                int quantity = Convert.ToInt32(qtyBox.Text);
                int unit = Convert.ToInt32(unitBox.Text);
                int no_of_app = Convert.ToInt32(noofappBox.Text);
                       
                decimal uprice = Convert.ToDecimal(unitBox.Text);
                Money unitprice = new Money(uprice) ;
                decimal discount = Convert.ToDecimal(discountBox.Text);
                decimal nprice = Convert.ToDecimal(netPriceBox.Text);
                Money netprice= new Money(nprice);
                
                
               // decimal val = quantity * unitprice;
               // decimal totalPrice = val - discount;
               
                // Used to update the data source.
               
                intelCRM.Attributes["new_itemcode"] = itemcode;
                intelCRM.Attributes["new_intelcrmquotationid"] = guid;
                intelCRM.Attributes["new_itemdescription"] = itemdesc;
                intelCRM.Attributes["new_quantity"] = quantity;
                intelCRM.Attributes["new_numberofapplications"] = unit;
                intelCRM.Attributes["new_unit"] = no_of_app;
                intelCRM.Attributes["new_discount"] = discount;
    
                intelCRM.Attributes["new_unitprice"] = unitprice.Value;
                intelCRM.Attributes["new_netprice"] = netprice.Value;
                
                    
                
                 crmService.Update(intelCRM);
               
    
                // Switch out of edit mode.
                dg_IntelCRM.EditItemIndex = -1;
             // dg_IntelCRM.DataBind();
                fillgrid();
                Response.Write("<script>alert('Your record is updated successfully')</script>");


    I am getting below error while executing the above code

    System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #CF866723

    please help me.

    Thanks

  • Verified answer
    ManirajKV Profile Picture
    1,477 on at

    Hi Habeeb,

    Issue is unitprice.value returns decimal value. but new_unitprice expects in money format. 

    So do like this : intelCRM.Attributes["new_unitprice"] = unitprice;

  • Habeeb-Dynamics CRM Application Developer Profile Picture
    on at

    here i have converted decimal to money using

    decimal nprice = Convert.ToDecimal(netPriceBox.Text);

               Money netprice= new Money(nprice);


    then adding the above money value in crm;

    intelCRM.Attributes["new_netprice"] = netprice.Value;

    if i don't need to get decimal value then how can we convert textbox type to money without using decimal type.

    Thanks

     

  • Habeeb-Dynamics CRM Application Developer Profile Picture
    on at

    Thanks Maniraj, its working...........

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans