简体   繁体   English

无法设置为'System.Decimal'值。 您必须将此属性设置为类型为“System.Double”的非null值

[英]could not be set to a 'System.Decimal' value. You must set this property to a non-null value of type 'System.Double'

Hi i am using mvc example to get data from a database. 您好我正在使用mvc示例从数据库中获取数据。 Here i got an error 我在这里得到了一个错误

Exception Details: System.InvalidOperationException: The 'number' property on 'Employee' could not be set to a 'System.Decimal' value. 异常详细信息:System.InvalidOperationException:'Employee'上的'number'属性无法设置为'System.Decimal'值。 You must set this property to a non-null value of type 'System.Double'. 您必须将此属性设置为类型为“System.Double”的非null值。

Description: An unhandled exception occurred during the execution of the current web request. 描述:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

Please see my code here, i got above error. 请在这里查看我的代码,我收到了上述错误。

public ActionResult Details(int id)
{
    EmployeeContext empcon = new EmployeeContext();
    Employee employ = empcon.employees.Single(emp => emp.empid == id);
    return View(employ);
}

Routiconfig Routiconfig

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    RouteTable.Routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "emp", action = "Details", 
                        id = UrlParameter.Optional }
        );
}

I think it might be because of null value conversion attempt to be stored in double. 我认为这可能是因为null值转换尝试存储在double中。

Change your double to 将你的double改为

double?

For explanation, please see the link below: http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx 有关说明,请参阅以下链接: http//msdn.microsoft.com/en-us/library/2cf62fcy.aspx

Thanks! 谢谢!

It may be the problem from DB ,from my experience I got this exception because I use data from store-procedure (generate object class using entity framework) 这可能是DB的问题,从我的经验来看,我得到了这个例外,因为我使用了store-procedure中的数据(使用实体框架生成对象类)

that one number field in my SP I supplying a data contract like this 在我的SP中的一个数字字段我提供这样的数据合同

CAST(NULL AS DECIMAL) AS UnitAmount

so in my code(after entity framework generate class) I got class that contain 所以在我的代码中(在实体框架生成类之后)我得到了包含的类

Decimal UnitAmount{get;set;}

but when I run my code this error happen 但是当我运行我的代码时,会发生此错误

...could not be set to a 'System.Int32' value. ...无法设置为'System.Int32'值。 You must set this property to a non-null value of type 'System.Decimal'. 您必须将此属性设置为类型为“System.Decimal”的非null值。

it's because in my SP there is one case condition that I return my result like this so the data type return is mismatch. 这是因为在我的SP中有一个案例条件,我返回我的结果,所以数据类型返回不匹配。

-- if no data
Select ... ,
       0  as UnitAmount, 
       ....

If you don't cast/convert the result 0 ->it will be seen as Int32 (0.00-> seen as Decimal ,) 如果你没有转换/转换结果0 - >它将被视为Int32(0.00->被视为十进制,)

Use 使用

public decimal number { get; set; } 

or 要么

public double number { get; set; }

link 链接

In my case the problem was in wrong DB column type - which was numeric and property was long . 在我的情况下,问题是错误的DB列类型 - 这是数字和属性很 So I just needed to recreate column as bigint . 所以我只需要将列重新创建为bigint

In my case it was a problem with the view, returning a value when there is a null value. 在我的情况下,它是视图的问题,当存在空值时返回值。

Had something like "ISNULL(dbo.STOCK_ITEMS.STDCOST, 0) AS STDCOST" 有“ISNULL(dbo.STOCK_ITEMS.STDCOST,0)AS STDCOST”之类的东西

Then did not work. 然后没有工作。

Tried "ISNULL(dbo.STOCK_ITEMS.STDCOST, 0.0) AS STDCOST" 尝试“ISNULL(dbo.STOCK_ITEMS.STDCOST,0.0)AS STDCOST”

Did not work too, but the following worked... 没有工作,但以下工作......

ISNULL(dbo.STOCK_ITEMS.STDCOST, 0.00) AS STDCOST

All working now.... 现在都在工作......

I met the similiar questions, said " could not be set to a 'Decimal' value. You must set this property to a non-null value of type 'Int32'." 我遇到了类似的问题,说“无法设置为'Decimal'值。您必须将此属性设置为'Int32'类型的非null值。” for one column. 一列。 and I search the whold solution, found I do set this column as Int in solution. 我搜索了解决方案,发现我在解决方案中将此列设置为Int。 But in database, it was set as money, instead of int. 但在数据库中,它被设置为钱,而不是int。 so I changed it in database level. 所以我在数据库级别更改了它。 and it works. 它的工作原理。

I was in a situation where I was unable to change the type of the column in the table (big project, lots of dependencies). 我遇到的情况是我无法更改表中列的类型(大项目,许多依赖项)。

I had a View feeding data into a Class definition, similar to this: 我有一个View将数据输入到Class定义中,类似于:

    SELECT Name,Phone,ID FROM People

Here, my ID was type int, but was being consumed as type string. 这里,我的ID是int类型,但是被用作类型字符串。 To solve this, I instead constructed my View like this: 为了解决这个问题,我改为构建了我的View:

    SELECT Name, Phone, CONVERT(NVARCHAR(2), ID) AS ContactMethodTypeId FROM People

Just tuck that CONVERT in around the problematic type and it will return as the type specified. 只是将CONVERT放在有问题的类型周围,它将返回指定的类型。 In my case, NVARCHAR. 就我而言,NVARCHAR。

A decimal in SQL is not the same as a decimal in .Net. SQL中的小数与.Net中的小数不同。 If you look at table of SqlDbType Enumeration you will find that SQL decimal is equivalent to double type. 如果查看SqlDbType枚举表,您会发现SQL十进制等效于double类型。

From C# References 来自C#参考文献

Float->Double. 浮子>双。 A floating point number within the range of -1.79E +308 through 1.79E +308. 浮点数在-1.79E +308到1.79E +308范围内。

public double? number { get; set; }

In my case changed code from float? 在我的情况下改变浮动代码? to double? 加倍? & it works. & 有用。

 public double? number { get; set; }

my database table column type is float. 我的数据库表列类型是float。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将“ y”的“ x”属性设置为“ System.Decimal”值。 您必须将此属性设置为“ System.Boolean”类型的非空值 - The 'x' property on 'y' could not be set to a 'System.Decimal' value. You must set this property to a non-null value of type 'System.Boolean' 将属性设置为“ System.Double”类型的非空值 - Set a property to a non-null value of type 'System.Double' Y的X属性不能设置为“十进制”值。 您必须将此属性设置为“ Single”类型的非空值 - The X property on Y could not be set to a 'Decimal' value. You must set this property to a non-null value of type 'Single' 无法将“表1”上的“ Id”属性设置为“ System.Byte []”值。 您必须将此属性设置为“ System.Guid”类型的非空值 - The 'Id' property on 'Table1' could not be set to a 'System.Byte[]' value. You must set this property to a non-null value of type 'System.Guid' 如何摆脱'属性无法设置为double值,必须将此属性设置为decimal类型的非null值' - How to get rid of 'property could not be set to a double value, you must set this property to a non-null value of type decimal' 无法将“ tblX”上的属性“ x”设置为“空”值。 您必须将此属性设置为'Int16'类型的非空值 - The property 'x' on 'tblX' could not be set to a 'null' value. You must set this property to a non-null value of type 'Int16' 带有 Informix 的实体框架; - 如何修复您必须将此属性设置为“Double”类型的非空值 - Entity Framework with Informix; - How to fix You must set this property to a non-null value of type 'Double' 无法设置为Double类型的非Double类型的非零值 - Could not be set to a Double value, non-null value of type Single 实体框架:“ IngramMicro”上的“ IngramMicro_RetailPrice”属性无法设置为“ System.Double”值 - Entity Framework: The 'IngramMicro_RetailPrice' property on 'IngramMicro' could not be set to a 'System.Double' value 无法将属性设置为字节值,必须将该属性设置为int32类型的非null - Property could not be set to a byte value you must set the property to a non null of type int32
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM