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

27,196

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),
Share:
27,196
Todd Wilson
Author by

Todd Wilson

Here to learn.

Updated on March 23, 2020

Comments

  • Todd Wilson
    Todd Wilson over 4 years

    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.

  • Todd Wilson
    Todd Wilson almost 10 years
    Thanks Tim. Let me try this.
  • Todd Wilson
    Todd Wilson almost 10 years
    Hi Tim. If you see my code there is a condition where TId can't be found in the db.HDGSAccTransaction In that case I am displaying a message about non-existence of this TId and "Back to Previous" link. Even for those transactions I am getting this error.
  • Tim Schmelter
    Tim Schmelter almost 10 years
    @ToddWilson: i don't see the code where you diplay a message. However, i don't understand how if (TransactionItem != null) is related to this. That just checks if TransactionItem is null, not if TransactionItem.IcV is null (or the other nullable properties).
  • Todd Wilson
    Todd Wilson almost 10 years
    @ Tim: That is when the null viewModel object goes to view and inside it I am checking if model is null. Its a view part. This is just to let you know that I am getting same error even if TransactionItem is null
  • Tim Schmelter
    Tim Schmelter almost 10 years
    @ToddWilson: i'm not that familiar with MVC, so i don't know what can also cause it in this method. TransactionID.Equals should not throw even if TransactionID is a nullable property which is null.
  • Chinh Phan
    Chinh Phan almost 9 years
    "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". Yes. You are right. But how come?
  • GlennG
    GlennG almost 6 years
    Great explanation and link to the demo. Really useful.