Hi i am using mvc example to get data from a database. Here i got an error
Exception Details: System.InvalidOperationException: The 'number' property on 'Employee' could not be set to a 'System.Decimal' value. You must set this property to a non-null value of type 'System.Double'.
Description: An unhandled exception occurred during the execution of the current web request. 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
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.
Change your double
to
double?
For explanation, please see the link below: 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)
that one number field in my SP I supplying a data contract like this
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. You must set this property to a non-null value of type 'System.Decimal'.
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.
-- 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 ,)
In my case the problem was in wrong DB column type - which was numeric and property was long . So I just needed to recreate column as 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"
Then did not work.
Tried "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'." for one column. and I search the whold solution, found I do set this column as Int in solution. But in database, it was set as money, instead of 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:
SELECT Name,Phone,ID FROM People
Here, my ID was type int, but was being consumed as type string. To solve this, I instead constructed my View like this:
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. In my case, NVARCHAR.
A decimal in SQL is not the same as a decimal in .Net. If you look at table of SqlDbType Enumeration you will find that SQL decimal is equivalent to double type.
From C# References
Float->Double. A floating point number within the range of -1.79E +308 through 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.