How to check null condition


#1

How to check null conditions? For example i have variable System.Data.Datatable datatable. If i write condition in if activity “datatable = Null”, then i get error that Null constant is not supported and i should use System.DbNull instead, but if i write condition “datatable = System.DbNull” then i get error that it is type.


#2

you can use
isNothing(dtYourTable)
this will return you a Boolean.

This other post should help you as well:


#4

@aksh1yadav this assumes that DataTable exists. You will get an object reference error if the Datatable does not exist and therefore you need the solution provided by @akhi_s27 prior to checking the number of rows.


#6

@aksh1yadav
creating datatable variable doesn’t ensure that the object is instantiated.
either variable declaration should be defaulted to new Datatable()
or a Build Datatable Activity should be used.
If a datatable variable is accessed without either of these, object reference exception will be thrown.
And if the object exists then as you said the row count should help for further analysis.


#8

Ui path wont give you error until you try to access properties or methods of that variable and i wanted to create check that if variable is not initialized, then i would create it and fill it with needed data.


#10

Hi , Actually we are reading a Datatable which has blank cells & If condition is not able to handle the empty cells in case any. Getting following error for the same

Please suggest


#11

this is the example excel


#12

Following code we are using it throws exception as soon as cell are empty


#13

have you tried the following?

if RI is nothing orelse RI.Equals("NameDT")


#14

Is there a way to find number of blank rows in a datatable


#15

One way would be to count the total number of rows then loop through and find the blanks (or non-blanks if easier) and subtract the total :slight_smile:


#16

@richarddenton

Doesn’t seem to work


#17

Hi,
“IsNothing()” or “=Nothing” is only going to work if there’s nothing assigned to that variable but looks like in your case that an actual row is there but it just contains “” values.

You could just see if certain values or columns has nothing in it like row(0)="", and that would tell you there’s nothing there.

Or you could look at query functions such as the .Select() which you can filter out the empty rows or whatever you need.


Deleting cell values - Error: Input string was not in a correct format
#18

Not tested, try this if nothing works

dt2 = dt1.Rows.Cast(Of DataRow)().Where(Function(row) Not row.ItemArray.All(Function(field) TypeOf field Is DBNull OrElse String.IsNullOrWhiteSpace(TryCast(field, String)))).CopyToDataTable()


#19

Ideally you should create a new topic for this as this is something new that you have asked.

(From dr As Datarow In YourDatatable
Where Array.TrueForAll(dr.ItemArray,Function(x)(String.IsNullOrWhiteSpace(x.ToString)))
Select dr).Count

This will return you count of blank rows in an integer.