The source contains no DataRows in

The source contains no DataRows error usually comes when the table comes empty.

The CopyToDataTable() method copies structure and Data, if you have any data in first datatable. Error comes when Linq filtered table is empty, and CopyToDataTable() tries to copy it.

For that there 2 solution i can suggest.
1. First Approach : Check if Enumerable result contains any row using "rows.Any()".
var rows = ds.Tables[0].AsEnumerable()
           .Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date);

   DataTable dt = rowSources.CopyToDataTable();
2. Second Approach : Use Clone() method to create an empty DataTable with the same schema as the source table and use a loop to fill it from the LINQ query.
DataTable merged = dt.Clone();
foreach (DataRow sourceRow in rows)
return merged;

CopyToDataTable() Returns a System.Data.DataTable that contains copies of the System.Data.DataRow objects, given an input System.Collections.Generic.IEnumerable object where the generic parameter T is System.Data.DataRow.

Post a Comment