Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. DBNull... why doesn't this work?!?!?!! ( IIF )

DBNull... why doesn't this work?!?!?!! ( IIF )

Scheduled Pinned Locked Moved Visual Basic
question
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mycroft Holmes

    Yah I knew I used it somewhere, try

    mvLogID = IIf(IsDBNull(oRow("LogID")), 0, oRow("LogID"))

    Never underestimate the power of human stupidity RAH

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    No luck. I'm not trying to find if a row is null, just if a cell is null

                first = IIf(IsDBNull(\_row.Cells(1)), 0, CDec(\_row.Cells(1).Value))
                second = IIf(IsDBNull(\_row.Cells(2)), 0, CDec(\_row.Cells(2).Value))
                final = IIf(IsDBNull(\_row.Cells(3)), 0, CDec(\_row.Cells(3).Value))
    

    Using the name instead of the index doesn't work either. Still get "Conversion from DBNull to type Decimal is not valid"

    M 1 Reply Last reply
    0
    • L Lost User

      No luck. I'm not trying to find if a row is null, just if a cell is null

                  first = IIf(IsDBNull(\_row.Cells(1)), 0, CDec(\_row.Cells(1).Value))
                  second = IIf(IsDBNull(\_row.Cells(2)), 0, CDec(\_row.Cells(2).Value))
                  final = IIf(IsDBNull(\_row.Cells(3)), 0, CDec(\_row.Cells(3).Value))
      

      Using the name instead of the index doesn't work either. Still get "Conversion from DBNull to type Decimal is not valid"

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #4

      Can I have my foot back now please :-O I was testing against a DATAROW. Try, you are testing the cell, it will always be there. And here is your foot :-D first = IIf(IsDBNull(_row.Cells(1).Value), 0, CDec(_row.Cells(1).Value))

      Never underestimate the power of human stupidity RAH

      L 1 Reply Last reply
      0
      • M Mycroft Holmes

        Can I have my foot back now please :-O I was testing against a DATAROW. Try, you are testing the cell, it will always be there. And here is your foot :-D first = IIf(IsDBNull(_row.Cells(1).Value), 0, CDec(_row.Cells(1).Value))

        Never underestimate the power of human stupidity RAH

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        Lol :-) Still no luck though. Here it is: (All of it)

        For Each _row As DataGridViewRow In dgvGrades.Rows
        Dim first, second, final As Decimal

                    ' \*\*\*\*\*\*\*\*\* This doesnt not seem to work but technically it should \*\*\*\*\*\*\*\*\*\*\*\*\*\*
                    ' \*\*\*\*\*\*\*\*\* Must be a FLAW in VB.net :-) lol \*\*\*\*\*\*\*\*\*\*\*\*
                    'first = IIf(TypeOf \_row.Cells(1).Value Is DBNull, 0, CDec(\_row.Cells(1).Value))
                    'second = IIf(TypeOf \_row.Cells(2).Value Is DBNull, 0, CDec(\_row.Cells(2).Value))
                    'final = IIf(TypeOf \_row.Cells(3).Value Is DBNull, 0, CDec(\_row.Cells(3).Value))
        
                    first = IIf(IsDBNull(\_row.Cells(1).Value), 0, CDec(\_row.Cells(1).Value))
                    second = IIf(IsDBNull(\_row.Cells(2).Value), 0, CDec(\_row.Cells(2).Value))
                    final = IIf(IsDBNull(\_row.Cells(2).Value), 0, CDec(\_row.Cells(3).Value))
        
                    Dim \_average As Decimal = (first + second + 2 \* final) / 4
        
                    For Each \_sRow As DataGridViewRow In dgvStudents.Rows
                        If \_row.Cells(0).Value Is Nothing Then
                            Exit For
                        Else
                            If \_row.Cells(0).Value = \_sRow.Cells(0).Value Then
                                dgvAverage.Rows.Add(New Object() {\_sRow.Cells(2).Value.ToString() + " " + \_sRow.Cells(1).Value.ToString(), \_average.ToString(), CalculateGrade(\_average), IIf(\_average > 59, True, False)})
                                Exit For
                            End If
                        End If
                    Next
                Next
        

        The funny thing is (well not so funny) is that I can do this:

        If IsDBNull(_row.Cells(1).Value) Then
        MessageBox.Show("IT IS NULL!")
        End If

        AND IT WORKS! its the same thing!

        N 1 Reply Last reply
        0
        • L Lost User

          Lol :-) Still no luck though. Here it is: (All of it)

          For Each _row As DataGridViewRow In dgvGrades.Rows
          Dim first, second, final As Decimal

                      ' \*\*\*\*\*\*\*\*\* This doesnt not seem to work but technically it should \*\*\*\*\*\*\*\*\*\*\*\*\*\*
                      ' \*\*\*\*\*\*\*\*\* Must be a FLAW in VB.net :-) lol \*\*\*\*\*\*\*\*\*\*\*\*
                      'first = IIf(TypeOf \_row.Cells(1).Value Is DBNull, 0, CDec(\_row.Cells(1).Value))
                      'second = IIf(TypeOf \_row.Cells(2).Value Is DBNull, 0, CDec(\_row.Cells(2).Value))
                      'final = IIf(TypeOf \_row.Cells(3).Value Is DBNull, 0, CDec(\_row.Cells(3).Value))
          
                      first = IIf(IsDBNull(\_row.Cells(1).Value), 0, CDec(\_row.Cells(1).Value))
                      second = IIf(IsDBNull(\_row.Cells(2).Value), 0, CDec(\_row.Cells(2).Value))
                      final = IIf(IsDBNull(\_row.Cells(2).Value), 0, CDec(\_row.Cells(3).Value))
          
                      Dim \_average As Decimal = (first + second + 2 \* final) / 4
          
                      For Each \_sRow As DataGridViewRow In dgvStudents.Rows
                          If \_row.Cells(0).Value Is Nothing Then
                              Exit For
                          Else
                              If \_row.Cells(0).Value = \_sRow.Cells(0).Value Then
                                  dgvAverage.Rows.Add(New Object() {\_sRow.Cells(2).Value.ToString() + " " + \_sRow.Cells(1).Value.ToString(), \_average.ToString(), CalculateGrade(\_average), IIf(\_average > 59, True, False)})
                                  Exit For
                              End If
                          End If
                      Next
                  Next
          

          The funny thing is (well not so funny) is that I can do this:

          If IsDBNull(_row.Cells(1).Value) Then
          MessageBox.Show("IT IS NULL!")
          End If

          AND IT WORKS! its the same thing!

          N Offline
          N Offline
          nlarson11
          wrote on last edited by
          #6

          you said "you get errors". what error are you getting? btw...not sure if you meant to do this but you are testing cell 2 again and then useing cell 3

          final = IIf(IsDBNull(_row.Cells(2).Value), 0, CDec(_row.Cells(3).Value))

          'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

          L 1 Reply Last reply
          0
          • N nlarson11

            you said "you get errors". what error are you getting? btw...not sure if you meant to do this but you are testing cell 2 again and then useing cell 3

            final = IIf(IsDBNull(_row.Cells(2).Value), 0, CDec(_row.Cells(3).Value))

            'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            The error is it can't convert dbnull to decimal. It shouldn't be trying because it's supposed to make it 0 if it was dbnull. Yeah I didn't mean to do that I just changed it so much trying to get it to work I messed that up

            N 1 Reply Last reply
            0
            • L Lost User

              The error is it can't convert dbnull to decimal. It shouldn't be trying because it's supposed to make it 0 if it was dbnull. Yeah I didn't mean to do that I just changed it so much trying to get it to work I messed that up

              N Offline
              N Offline
              nlarson11
              wrote on last edited by
              #8

              I have seen that somestimes checking for dbnull in an IIF doesn't always work. try breaking it out into a normal IF/Else statement.

              'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

              L 1 Reply Last reply
              0
              • N nlarson11

                I have seen that somestimes checking for dbnull in an IIF doesn't always work. try breaking it out into a normal IF/Else statement.

                'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #9

                Well I know that it works fine with a bunch of if then statements. I was trying to figure out if it was something I was doing why the IIf wasn't working. But its starting to look more like a flaw with VB.net. Using the IIf statements looks much cleaner and less code to me. But if it just won't work with comparing DBNull values.. then what can I do lol

                N 1 Reply Last reply
                0
                • L Lost User

                  Well I know that it works fine with a bunch of if then statements. I was trying to figure out if it was something I was doing why the IIf wasn't working. But its starting to look more like a flaw with VB.net. Using the IIf statements looks much cleaner and less code to me. But if it just won't work with comparing DBNull values.. then what can I do lol

                  N Offline
                  N Offline
                  nlarson11
                  wrote on last edited by
                  #10

                  ya it's a bug... you can get the same effect by writing a function that will act like the IIF so you'll get the clean look but the function will use the full if/then/else...

                  't = true part of iif / e = else part of iif
                  public function MyIIF (b as boolean, t as object, e as object) as object

                  you can of course overload that to do type-safe checks

                  public function MyIIF (b as boolean, t as string, e as string) as object
                  public function MyIIF (b as boolean, t as integer, e as integer) as object

                  etc

                  'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

                  L 1 Reply Last reply
                  0
                  • N nlarson11

                    ya it's a bug... you can get the same effect by writing a function that will act like the IIF so you'll get the clean look but the function will use the full if/then/else...

                    't = true part of iif / e = else part of iif
                    public function MyIIF (b as boolean, t as object, e as object) as object

                    you can of course overload that to do type-safe checks

                    public function MyIIF (b as boolean, t as string, e as string) as object
                    public function MyIIF (b as boolean, t as integer, e as integer) as object

                    etc

                    'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    Ok Thanks for your help :-)

                    1 Reply Last reply
                    0
                    • L Lost User

                      I cannot figure out why this doesn't work. Everythign says it should... I'm trying to see if a datagridviewrow contains DBNUll

                                  'first = IIf(TypeOf \_row.Cells(1).Value Is DBNull, 0, CDec(\_row.Cells(1).Value))
                                  'second = IIf(TypeOf \_row.Cells(2).Value Is DBNull, 0, CDec(\_row.Cells(2).Value))
                                  'final = IIf(TypeOf \_row.Cells(3).Value Is DBNull, 0, CDec(\_row.Cells(3).Value))
                      

                      I GET ERRORS!!! But if I do this, not only is it ugly as heck but it works!!

                                  If (TypeOf \_row.Cells(1).Value Is DBNull) Then
                                      first = 0
                                  Else
                                      first = \_row.Cells(1).Value
                                  End If
                      
                                  If (TypeOf \_row.Cells(2).Value Is DBNull) Then
                                      second = 0
                                  Else
                                      second = \_row.Cells(2).Value
                                  End If
                      
                                  If (TypeOf \_row.Cells(3).Value Is DBNull) Then
                                      final = 0
                                  Else
                                      final = \_row.Cells(3).Value
                                  End If
                      

                      Why?!?! I'm so angry. I have tried everything from

                      IsDBNull(value)

                      to

                      value Is DbNull
                      value Is DbNull.Value

                      to

                      value is typeof(DbNull)

                      Its just frustating.. am I doing something wrong here or is this just a flaw?

                      J Offline
                      J Offline
                      Jack Vanderhorst
                      wrote on last edited by
                      #12

                      If you're using VB9 there is an overload for the If keyword that solves this problem. Instead of IIf try just using If

                      1 Reply Last reply
                      0
                      • L Lost User

                        I cannot figure out why this doesn't work. Everythign says it should... I'm trying to see if a datagridviewrow contains DBNUll

                                    'first = IIf(TypeOf \_row.Cells(1).Value Is DBNull, 0, CDec(\_row.Cells(1).Value))
                                    'second = IIf(TypeOf \_row.Cells(2).Value Is DBNull, 0, CDec(\_row.Cells(2).Value))
                                    'final = IIf(TypeOf \_row.Cells(3).Value Is DBNull, 0, CDec(\_row.Cells(3).Value))
                        

                        I GET ERRORS!!! But if I do this, not only is it ugly as heck but it works!!

                                    If (TypeOf \_row.Cells(1).Value Is DBNull) Then
                                        first = 0
                                    Else
                                        first = \_row.Cells(1).Value
                                    End If
                        
                                    If (TypeOf \_row.Cells(2).Value Is DBNull) Then
                                        second = 0
                                    Else
                                        second = \_row.Cells(2).Value
                                    End If
                        
                                    If (TypeOf \_row.Cells(3).Value Is DBNull) Then
                                        final = 0
                                    Else
                                        final = \_row.Cells(3).Value
                                    End If
                        

                        Why?!?! I'm so angry. I have tried everything from

                        IsDBNull(value)

                        to

                        value Is DbNull
                        value Is DbNull.Value

                        to

                        value is typeof(DbNull)

                        Its just frustating.. am I doing something wrong here or is this just a flaw?

                        D Offline
                        D Offline
                        Dougie the P
                        wrote on last edited by
                        #13

                        When using IIF both the true and the false portions are evaluated before the IIF test is done. using the If construct only the true portion is evaluated after the IF test is done and the false portion is never evaluated if the test is true.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups