Search code examples
c#sql-servertype-conversionsqldatatypes

Convertion decimal sql type to .net float type


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?


Solution

  • 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; }