🚑 🆘 [FirstAid] Handling of The source contains no DataRows exception

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
Visuals

Result and Exception

Success

When the data table will be filtered for “fr” the following result will be received:
grafik

Failure

When the data table will be filtered for “uk” a the source contains no DataRows EXCEPTION will be thrown
grafik

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
grafik

Details

grafik
grafik


then click OK, OK

LINQ Statement & Assign Change

instead of CopyToDatatable a ToList will be used
The return of the LINQ will be assigned to the Variable: Result
grafik

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

grafik

grafik

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
grafik

Details

grafik
grafik


then click OK, OK

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

grafik

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)

Questions

For questions on your specific case open a new topic and get individual support

10 Likes
Error Linq query
The source contains no DataRows
How to handle this
LINQ Get rows from DT1 that are in DT2
How to delete particular row in excel?
Issues with Except/Remove dublicate rows in two datatables
Problem with empty datarow
Matching dates Using Linq Query
Delete or Remove an entire Row in excel on the basis of some Condition
How do i Encounter this Assign: The source contains no DataRows Error
Quick Guidance Please
Forum Engagement Daily Reports
Filter DataTable End with Numbers
Use linq to get different values between two tables
Remove data row if the value cannot be find in an array
Issue with linq query of getting unmatched data
How to Filter data table from specific time given?
How to get the older dates than the current month
Printing Specific Columns
How to Filter non numeric value on datatable using linq
Why row is not there in context error coming when using group by function any one please help me
LINQ Query Dynamic Column
LINQ Query Specific Data Retrieval
Compare 2 datatables with 3 columns and get unmatched records
Compare two datatables and get unique value from dt1
How To fix "The source contains no DataRows."?
Remove row from a particular string from data Table
How to use ternary without else?
Assign - the source contains no data row - problem with extracting data from empty datatable
Assign - the source contains no data row - problem with extracting data from empty datatable
Need help to Filter dates older than a particular date
Getting error when adding two conditions
How to filter rows in DT for same value in two columns?
Join data tables question
I Need To filter excel data column by using LINQ query
Comparing 2 Data tables
Assign Activity LINQ Query
Catch Error Linq query to check datatable
Source contains no datarows
Code taking long time to execute
I have datatable and a list of string (having years) need a select query to fetch those records from datatble that is containing the items from list
Linq query to filter data table
Date Time Format change and comparison
Group data and sum only one of the groups in excel
Left Join Datatable with lenght
To perform sum if column data is matched
How to delete the row from datatable2 which has different value from datatable1
Assign:The source contains no datarows?
Comparing the two datatables for exception of common value
Filter data table using .AsEnumerable
How do i delete empty rows after i read range?
I Want to Sort Date Column With a Dynamic Two dates
Compare column is equal or not
Filter Datatable By All Items In Collection List
Filter few columns with LINQ
Error when I am trying to filters rows in data table using LINQ query when there is no value with search name
Filterdatatable convert string into Int
How to search and list (filter) and show all result in Excel
Select dt empty rows - The source contains no DataRows
Datatable comparision -- between two DTs and get the common records
How to handle "The source contains no datarows" exception?
How to filter value using query instead of filter data table
Filter a data table based on a column in another data table
Finding the similar columns between two different sheets
Filter datatable dynamically using DataTable.AsEnumerable()
How to filter exact values
Delete empty rows in excel

A post that was very much needed in this community, thank you for taking the time to write this for us, we appreciate it!

Kind Regards,
Ashwin A.K

1 Like

Great post @ppr! Just wanted to add that you can also merge it all in one expression if you don’t want to use several activities.

(From i In {0}
	Let matchingRows = dtData.AsEnumerable.Where(Function (x) x("CCode").toString.Trim.ToUpper.Equals(strCCFilter.ToUpper))
	Select If (matchingRows.Count > 0, matchingRows.CopyToDataTable, dtData.Clone)
).First

image

5 Likes

Hi Peter!
Thank you for this post. I was really looking for something like that!
Cheers,

This was cool, But in my scenario in filtering datatable, if i had 2 rows, and those 2 are filtered and my datatable is beoming blank, how to handle this scenario ,

I am deleliting all filtered data, so my filterdatatable is trowing error.

If you use my LINQ-expression you should have a an empty data table (with correct headers but no rows.) That is was the dtData.Clone does.

How do you filter your datatable? Could you show us your LINQ-expression?

@MitheshBolla @ptrobot

we would recommend to open a new topic for this discussion

2 Likes