Search code examples
asp.net-mvcvb.netrazornullableinvalidoperationexception

Decimal? - Nullable object must have a value


I'm using VB.NET MVC 5.1 in VS2013 using Razor

siq.Price is a Decimal?

I'm trying to check if siq.Price has a value, and if so print it as a currency. I recieve this error when trying to access the .Value property for elements where true they returned true for .HasValue:

Nullable object must have a value.

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.

Exception Details: System.InvalidOperationException: Nullable object must have a value.

I tried both of these lines of code, believing they should work without issue, yet recieved the error above:

@IIf(siq.Price.HasValue, siq.Price.Value, "N/A")
   'and
@IIf(siq.Price.HasValue, siq.Price.Value.ToString("C"), "N/A")

As soon as I remove the .Value from the TruePart then it started working:

@IIf(siq.Price.HasValue, siq.Price, "N/A")
    'and
@IIf(siq.Price.HasValue, String.Format("{0:C}", siq.Price), "N/A")

Can anyone explain why this is happening?! I'm rather perplexed.

NOTE: This piece of code works on it's own if the Price has a value, but not in the IIf():

@siq.Price.Value.ToString("C")

Solution

  • The IIF() function in VB.Net always evaluates both the true and false parts. Instead, you should change to use the IF() operator, which only evaluates the return value, so something like:

    @If(siq.Price.HasValue, siq.Price.Value.ToString("C"), "N/A")