Error if there is no row at position 0

I user filter status dataTable but sometime after filter don’t have data that match with condiotion.

So error as below.

Condition in if = dt_input_wrongCat_RowCount <=1 andalso dt_input_wrongCat.Row(0).itemArray.all(Function(x) string.IsnullOrEmpth(x.tostring.Trim))

if there is no row at position 0

Please guide me for solve it.

Thank you

@fairymemay

When you filter a DataTable and the resulting DataTable does not have any rows, accessing the first row (dt_input_wrongCat.Rows(0)) will cause an error,To prevent the error check if the DataTable has any rows before trying to access a specific row.

If dt_input_wrongCat.Rows.Count = 0 OrElse (dt_input_wrongCat.Rows.Count <= 1 AndAlso dt_input_wrongCat.Rows(0).ItemArray.All(Function(x) String.IsNullOrEmpty(x.ToString.Trim))) Then
    ' Handle the case where the DataTable is empty or the first row has all empty cells
    ' Your logic here
Else
    ' Handle the case where the DataTable has the required data
    ' Your logic here
End If

1 Like

Hi @fairymemay

Your DataTable is returning no rows and you are using index 0 in your condition,
Hence it is failing, you need to check for rowCount>0 first and then use other queries.

Thanks
Happy Automation! :smiley:

1 Like

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