1

I have decimal (18,2) data in sql and I set float the property of class in .net side. When I select the object .net returns an error like this:

The 'DiscountRate' property on 'Product' could not be set to a 'Decimal' value. You must set this property to a non-null value of type 'Single'.

Do you have suggestion?

cagin
  • 5,772
  • 14
  • 74
  • 130

4 Answers4

2

In your .NET code double or decimal type for decimal(18, 2).

See here for a cross reference from SQL to .NET types.

Now if you decimal column can be null make sure you use the nullable decimal or

public decimal? DiscountRate { get; set; }
David Basarab
  • 72,212
  • 42
  • 129
  • 156
0

I would suggest that you need to either change your property in your .NET class to a decimal type or do an explicit conversion to a float when setting it. Which you use is really dependant on your code though using a decimal type seems more correct without any further context.

Chris
  • 27,210
  • 6
  • 71
  • 92
0

You can use Decimal Type

Link : http://msdn.microsoft.com/en-us/library/system.decimal.aspx

You can read Specifying Parameter Data Types section

Link : http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
0

You should change the property to decimal. It makes conversion easier and protects you from rounding errors that come with float or double.

Here's an extensive discussion of the issue: decimal vs. double

Community
  • 1
  • 1
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108