Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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 }
        );
}
like image 752
stpdevi Avatar asked Jan 06 '14 10:01

stpdevi


3 Answers

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!

like image 111
Hatjhie Avatar answered Nov 17 '22 05:11

Hatjhie


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 ,)

like image 29
user3682728 Avatar answered Nov 17 '22 06:11

user3682728


Use

public decimal number { get; set; } 

or

public double number { get; set; }

link

like image 6
Nagaraj S Avatar answered Nov 17 '22 04:11

Nagaraj S