I have this code in a shopping cart I am creating but I am receiving the error shown in the title.The error is showing up on this line:
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- MVC-Routing,Why i can not ignore defaults,The matc
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
It sounds like the
Order
class has aTotal
property that's a string. Check the class definition forOrder
. If that's the case, I'd recommend changing theTotal
data type to a decimal, because having a currency value represented as a string doesn't make sense.decimal
makes more sense.Change the type of
Order.Total
to Decimal. The data type Decimal does not automatically convert to string (and visa-versa) which is why you're getting an error, plus, any subsequent calls toOrder.Total
would be string related, so you would not be able to use operators likeOrder.Total +=
Also note that any binding convenience like a DataTable's 'format' property would be ignored because the underlying architecture won't call string.ToString(format).
How is your Order class looks like ? What is the type of the Property called
Total
? It looks like it is a string type. Here you are trying to set decimal value to a string type That is y you are getting that error. So you need to convert decimal to string before assigning it to a string varaiable. Use thetoString()
method.As Tudor already mentioned, It is better to use Total as a decimal property as it will be easy for you to do all mathematical operations. You dont need to convert it to decimal format every time you want to do an arithmetical operation and convert it again back to string
Just call
ToString
to get a string representation of the decimal value:Although I don't see why you would store the member
Total
as astring
and not as adecimal
too.