locked
Catching NULL Values RRS feed

  • Question

  • User-1380038293 posted

    Hi,

    I am LINQ querrying against a datatable. However, I am unsuccessfull with catching null values. Here is my code that is giving me the following error.

     

                Dim query = From line In dtCS.AsEnumerable()
                            Where line.Field(Of Integer)("creditorid") = id
                            Select New With {.minperc = IIf(IsDBNull(line.Field(Of Double)("minimumpercent")), 0, line.Field(Of Double)("minimumpercent")),
                                            .minamt = line.Field(Of Object)("minimumamount"),
                                            .perof = line.Field(Of Integer)("percentof")}

    I am checking to see if minperc is null. However to check that it still is trying to convert the value to double. Cant figure out a way around it.

     

    Here is the error:

    Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type.


     

    Wednesday, April 21, 2010 1:09 PM

Answers

  • User-311685349 posted

    I'm just going to take a guess here, would

  • Dim query = From line In dtCS.AsEnumerable()   
  •             Where line.Field(Of Integer)("creditorid") = id   
  •             Select New With {.minperc = IIf(IsDBNull(line.Field(Of Double?)("minimumpercent")), 0, line.Field(Of Double)("minimumpercent")),   
  •                             .minamt = line.Field(Of Object)("minimumamount"),   
  •                             .perof = line.Field(Of Integer)("percentof")} 

     

    I would suggest it's unable to cast a null to a double on the highlighted line, so it can't actually run the test.  I'm not familar with VB, so I'm not sure if "?" would help or maybe just a simple "object" type would do to allow it to attempt it's test

  • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 21, 2010 7:36 PM

All replies

  • User-311685349 posted

    I'm just going to take a guess here, would

  • Dim query = From line In dtCS.AsEnumerable()   
  •             Where line.Field(Of Integer)("creditorid") = id   
  •             Select New With {.minperc = IIf(IsDBNull(line.Field(Of Double?)("minimumpercent")), 0, line.Field(Of Double)("minimumpercent")),   
  •                             .minamt = line.Field(Of Object)("minimumamount"),   
  •                             .perof = line.Field(Of Integer)("percentof")} 

     

    I would suggest it's unable to cast a null to a double on the highlighted line, so it can't actually run the test.  I'm not familar with VB, so I'm not sure if "?" would help or maybe just a simple "object" type would do to allow it to attempt it's test

  • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 21, 2010 7:36 PM
  • User-208454459 posted

    The problem is that IIF always executes the "true" and "false" actions. You should try the If() replacement of IIF which only executes one or the other.

    Sunday, March 11, 2012 6:20 PM