Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.InvalidOperationException: Nullable object must have a value.ASP.NET MVC

I am tired of fixing this error but nothing is working.

Here is the error:

System.InvalidOperationException: Nullable object must have a value.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Nullable1.get_Value() at Finance.Calculator.Presentation.Admin.Areas.HDGS.Controllers.TransactionController.Edit(Int64 TransactionId, String returnURL) in d:\BuildAgent2\work\83d988abf03ace44\Code\Presentation\Finance.Calculator.Presentation.Admin\Areas\HDGS\Controllers\TransactionController.cs:line 44 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.b__14() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

Here is my Code:

public ViewResult Edit(string TId, string returnURL)
    {
        long tid = !string.IsNullOrEmpty(TId) ? Convert.ToInt64(TId) : Convert.ToInt64(Request.QueryString["TId"].ToString());
        var TransactionItem = db.HDGSAccTransaction.SingleOrDefault(t => t.TransactionID.Equals(tid));
        TransactionVM oTrns = null;
        if (!string.IsNullOrEmpty(returnURL))
        {
            TempData["ReturnUrl"] = returnURL;
        }
        if (TransactionItem != null)
        {
            oTrns = new TransactionVM
            {
                TRef = TransactionItem.dimTransaction.TRef,
                Yr = TransactionItem.Yr,
                IcV = (decimal)TransactionItem.IcV,
                OFv = (decimal)TransactionItem.OFv,
                OCBv = (decimal)TransactionItem.OCBv,
                OOBv = (decimal)TransactionItem.OOBv,
                OInv = (decimal)TransactionItem.OInv
            };
        }

        return View(oTrns);
    }

Please Note: This code runs fine in the development environment from Visual Studio but not on the servers when it is deployed.

like image 560
Todd Wilson Avatar asked Jul 03 '14 07:07

Todd Wilson


People also ask

What does it mean when it says Nullable object must have a value?

This error occurs when you blindly call Value on a nullable type. Make sure you have a value on the nullable type (using HasValue) before calling Value on it.

What is a nullable object?

In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral ("null") behavior.

What is C# HasValue?

HasValue); // Print True. Value: This property gives the value of the Nullable type variable. If the variable has some value, it will return the value; else, it will give the runtime InvalidOperationException exception when the variable value is null. Nullable<int> a = null; Console. WriteLine(a.


1 Answers

I guess the reason why it's only working on your development environment is that the variables always have a value there and on your server the nullables are null.

If a Nullable<T> is null you cannot cast it explicitly to the value type because you get your exception "Nullable object must have a value". (Demo)

So don't cast it. What value do you want to use instead of null, you could use decimal.MinValue:

// ...
IcV = TransactionItem.IcV.HasValue ? TransactionItem.IcV.Value : decimal.MinValue,
// ...

For what it's worth, here is an extension method:

public static T SafeCast<T>(this Nullable<T> t, T defaultValue = default(T)) where T : struct
{
    return t.HasValue ? t.Value : defaultValue;
}

Then you can from now on use this:

IcV = TransactionItem.IcV.SafeCast(decimal.MinValue),
like image 152
Tim Schmelter Avatar answered Sep 21 '22 02:09

Tim Schmelter