How to check null condition

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 Likes

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

This other post should help you as well:

6 Likes

@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.

2 Likes

@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.

1 Like

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.

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

this is the example excel

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

have you tried the following?

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

2 Likes

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

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:

@richarddenton

Doesn’t seem to work

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.

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()

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.

2 Likes