How to convert null value in int zero

I am getting null value in excel sheet now I want to convert in zero so I can apply if condition

Hi @Aditya10989,
You can even check if there is null or not for if condition instead of int like…
String.isNullOrEmpty(value).
Cheers.
Vashisht.

3 Likes

@Vashisht already try but work my variable is in int form

@Aditya10989 - you can check in if like variable = NULL

I am attaching you screen shoot my chkamt is int form if I am getting null value from excel sheet then it convert to zero if Convert.ToInt32(row.Item(21)) this is null then it show the value zero

Can you check type of the variable using row.item(21).GetType before converting to integer.

@sandipauti I am getting system.DBNull now I want to convert it int zero

@Aditya10989
you can try below…
CheckAmt = if(String.IsNullorEmpty(row.item(21).Tostring) ,0,Cint(row.item(21).ToString))

1 Like

@AkshaySandhu thanks for reply done using Convert.ToInt32(IsDBNull(row.Item(21).ToString))

this wont work…
because you are converting Boolean value to int…
when row.Item(21).ToString = System.DbNull
your function will always produce = 1
else
your function will always produce = 0
it wont convert row.Item(21).ToString value to Int…

1 Like

@AkshaySandhu yes thanks you right.right now I am always getting zero value let me apply your code

@AkshaySandhu thanks for help you are right

and one more thing…
if you are getting check amount in decimal value, use below instead…

if(String.IsNullorEmpty(row.Tostring) ,0,Convert.ToDecimal(row.ToString))

@Aditya10989

Will you please explain this syntax.
Is this a different syntax for if ?

“If” structure is the following:

if( condition, value if condition is true, value if condition is false).

so, in if(String.IsNullorEmpty(row.item(21).Tostring) ,0,Cint(row.item(21).ToString))
when String.IsNullorEmpty(row.item(21).Tostring) → string is empty
return 0
else return row.item(21) value

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.