How can I save values from a textbox (input == text) as Currency values in Sharepoint?

I have Sharepoint list values that are Currency, such as:

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

But how do I save these values into the list as the Currency type?

I tried this:

spli["Airfare"] = (SPFieldType.Currency) boxAirfare.Text;

..and this:

spli["Airfare"] = boxAirfare.Text as SPFieldType.Currency;

...but they won't compile, telling me, "'Microsoft.SharePoint.SPFieldType.Currency' is a 'field' but is used like a 'type'"

I then tried to Convert the val, but found, much to my disjune (it's too late in the year for dismay), that there is no Currency type there. And even what seems to be the next best (or even better, if not for this) choice, namely Decimal, hints at a dismal destiny for such an attempt:

enter image description here

Is this true -- that converting to Decimal always fails? Note: "Convert.ToDouble" exudes the same dire warning.

Am I doomed to store the vals as String, and do the hocus pocus as necessary to treat them as money vals?

Note: A related question about which data type to use in Sharepoint for money is here

Upvotes: 1

Views: 431

Answers (1)

Colin
Colin

Reputation: 4135

Convert.ToDouble() should work fine (or double.Parse()).

The warning you see is related to trying to convert a DateTime to a decimal/double. If you look at the overloaded version that converts a string, I suspect you'll see no such warning.

Try:

//assumes input has been validated
spli["Airfare"] = double.Parse(boxAirfare.Text);

Upvotes: 1

Related Questions