3

I want to handle null condition in below code.

    lstTest.Discount = If((Not dataSet.Tables("History") Is Nothing), 
If(IsDBNull(dataSet.Tables("History").Rows(0)("DiscountsAdjustmentsAmount")),
 "$0.00", 
StringToCurrency(GetContractualDiscount(dataSet.Tables("History").Rows(0)
("DiscountsAdjustmentsAmount"), dataSet.Tables("History").Rows(0)
("DiscountsAdjustments"), dataSet.Tables("History").Rows(0)
("EstimatedCharges")))), "$0.00")

My code is getting break at

dataSet.Tables("History").Rows(0)("DiscountsAdjustments")

since its value is null. I want to replace null value with "0.00"

Please help how can I handle.

Thanks

Claudius
  • 1,883
  • 2
  • 21
  • 34
rahul aggarwal
  • 143
  • 1
  • 11
  • Why don't you use multiple lines? It would increase readability, maintainability and testability enormously. It would also be easier to fix such issues. – Tim Schmelter Mar 18 '16 at 13:03
  • It was coded earlier, can't change the code – rahul aggarwal Mar 18 '16 at 13:04
  • I can modily it whole, it error needs to be resolved – rahul aggarwal Mar 18 '16 at 13:05
  • 2
    No offense, but the code is really a complete mess. You should not abuse the conditonal operator in this way. You should also use the correct types instead of always object and string. The `DataRow` class has the `Field(Of T)` extension method that also supports nullable types. This sounds as if the column should be a `Decimal?`. Then you could get the value in this way: `Dim discountsAdjustments = dataSet.Tables("History").Rows(0).Field(Of Decimal?)("DiscountsAdjustments")` – Tim Schmelter Mar 18 '16 at 13:10
  • You should check if `dataSet.Tables("History").Rows(0))` is null before attempting to get `dataSet.Tables("History").Rows(0)("DiscountsAdjustmentsAmount")` – Scott Marcus Mar 18 '16 at 13:16

2 Answers2

2

Rahul,

You will likely need to rewrite this part of it. Here is your original code:

   lstTest.Discount = If((Not dataSet.Tables("History") Is Nothing), 
If(IsDBNull(dataSet.Tables("History").Rows(0)("DiscountsAdjustmentsAmount")),
 "$0.00", 
StringToCurrency(GetContractualDiscount(dataSet.Tables("History").Rows(0)
("DiscountsAdjustmentsAmount"), dataSet.Tables("History").Rows(0)
("DiscountsAdjustments"), dataSet.Tables("History").Rows(0)
("EstimatedCharges")))), "$0.00")

Instead of this big nested mess... why not do it this way. Note I dont have a VB debugger in front of me so there may be some slight format adjustments, so consider this pseudo code:

Is the dataset valid

If Not IsDBNull(dataSet.Tables("History"))

''We know that we have data in our dataset

''Do all your checks 
if Not isDBNull(dataSet.Tables("History").Rows(0)("Your field"))
 ''Do something
Else
  ''Show a 0
END IF

''REPEAT THE ABOVE LINES FOR EACH FIELD

End if
logixologist
  • 3,694
  • 4
  • 28
  • 46
0

You can check for null on any column first using methods on the DataRow object:

Which of IsDBNull and IsNull should be used?

Community
  • 1
  • 1
Brian Mains
  • 50,520
  • 35
  • 148
  • 257