DBNull... why doesn't this work?!?!?!! ( IIF )
-
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.Valueto
value is typeof(DbNull)
Its just frustating.. am I doing something wrong here or is this just a flaw?
-
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.Valueto
value is typeof(DbNull)
Its just frustating.. am I doing something wrong here or is this just a flaw?
Yah I knew I used it somewhere, try
mvLogID = IIf(IsDBNull(oRow("LogID")), 0, oRow("LogID"))
Never underestimate the power of human stupidity RAH
-
Yah I knew I used it somewhere, try
mvLogID = IIf(IsDBNull(oRow("LogID")), 0, oRow("LogID"))
Never underestimate the power of human stupidity RAH
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"
-
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"
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
-
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
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 IfAND IT WORKS! its the same thing!
-
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 IfAND IT WORKS! its the same thing!
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
-
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
-
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
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
-
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
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
-
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
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 objectyou 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 objectetc
'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
-
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 objectyou 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 objectetc
'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
-
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.Valueto
value is typeof(DbNull)
Its just frustating.. am I doing something wrong here or is this just a flaw?
If you're using VB9 there is an overload for the If keyword that solves this problem. Instead of IIf try just using If
-
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.Valueto
value is typeof(DbNull)
Its just frustating.. am I doing something wrong here or is this just a flaw?
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.