This FirstAid Tutorial will describe how a the source contains no DataRows EXCEPTION can be handled.
Introduction
Let’s have a look at the following filter data table scenario:
Name | CCode |
---|---|
Tom | US |
Charlotte | FR |
Seema | IN |
Jean | FR |
Assignment:
- Filter all rows on a particular Country Code like FR, UK, ES, UK…
- Ensure that all CCode data column values are trimmed
- Ensure that the Filter check is case insensitive
we can implement it e.g. with the help of a LINQ statement:
dtData.AsEnumerable.Where(Function (x) x("CCode").toString.Trim.ToUpper.Equals(strCCFilter.ToUpper)).CopyToDataTable
Result and Exception
Success
When the data table will be filtered for “fr” the following result will be received:
Failure
When the data table will be filtered for “uk” a the source contains no DataRows EXCEPTION will be thrown
Reason: when the filter result will be empty, there are no rows for copying it to the resulting data table.
Defensive Handling
Strategy
The main idea for defensive handling is about collecting all filtered rows to a list. Depending on the list count the CopyToDataTable method will be called or the original data table will be cloned
Fixing Actions - LINQ
Variables
For the pre-step result, a variable Result of the datatype: List(of DataRow) will be created
LINQ Statement & Assign Change
instead of CopyToDatatable a ToList will be used
The return of the LINQ will be assigned to the Variable: Result
Defensive Check Block
Within an IF Activity following will be modeled:
Condition: Result.Count > 0
Then Branch: dtResult = Result.CopyToDataTable
Else Branch: dtResult = dtData.Clone
dtData is the origin datatable. With the Clone Method an empty data table with the same data column structure of dtData will be created and assigned to dtResult
Visual
Fixing Actions - DataTable.Select Method
Similar to the above-described steps (Fixing Actions - LINQ) a defensive handling can also be
implemented for the DataTable.Select Method approach:
Variables
For the pre-step result, a variable Result of the datatype: List(of DataRow) will be created
Assignment
Result = dtData.Select("..YourSelectFilterExpression..").ToList()
Defensive Check Block
Within an IF Activity following will be modeled:
Condition: Result.Count > 0
Then Branch: dtResult = Result.CopyToDataTable
Else Branch: dtResult = dtData.Clone
Visual
dtData is the origin datatable. With the Clone Method an empty data table with the same data column structure of dtData will be created and assigned to dtResult
Conclusion
with the implementation of the above an empty filter result can be handled defensively and a the source contains no DataRows EXCEPTION can be avoided.
Downloads
Find starter help here:
ppr_FirstAid_NoSourceRows.xaml (8.4 KB)
References
The first aid series offers various instructions on how to tackle a difficulty with the first steps
Questions
For questions on your specific case open a new topic and get individual support