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 ,)
Use
public decimal number { get; set; }
or
public double number { get; set; }
link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With