Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle Null condition

Tags:

asp.net

vb.net

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

like image 496
rahul aggarwal Avatar asked Apr 09 '26 14:04

rahul aggarwal


1 Answers

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
like image 130
logixologist Avatar answered Apr 12 '26 03:04

logixologist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!