Search code examples
c#castingsharepoint-2010decimalsharepoint-list

How can I save my Sharepoint list value as currency/decimal?


I have an item/member of a Sharepoint 2010 list that is declared as data type CUrrency:

list.Fields.Add("Section5Total", SPFieldType.Currency, false);

Yet, when I try to save a currency value into that member, it won't compile. This:

spli["Section5Total"] = (SPFieldCurrency)boxSection5Total.Text;

...gives me,

Cannot convert type 'string' to 'Microsoft.SharePoint.SPFieldCurrency'

This:

spli["Section5Total"] = (decimal)boxSection5Total.Text;

...similarly tells me I can't convert String to Decimal.

I get that the compiler is not going to look at "Section5Total" and figure out that it has been declared as a Currency SPFieldType. Yet this:

spli["Section5Total"] = 0.0;

...compiles just fine. So why does it accept a literal float val, but not a cast one?


Solution

  • You cannot cast a string to decimal. Try:

    spli["Section5Total"] = Convert.ToDecimal(boxSection5Total.Text);