Thursday, January 3, 2013

Avoid "Source contains no data rows" Exception in DataTable


I was using following code to get a filtered DataTable

DataTable DT = new DataTable();

DT = NewMobileFa.GetNewMobile().AsEnumerable().Where(r => arrList.Contains(r.Field<int>("NewMobileID")) && r.Field<string>("Status") == "OF").CopyToDataTable();

Above code gave me the error Source contains no data rows. Which is correct and actually there is no data.

I needed to avoid the exception even though there is no data. Below code solved the issue.

DT = NewMobileFa.GetNewMobile().AsEnumerable().Where(r => arrList.Contains(r.Field<int>("NewMobileID")) && r.Field<string>("Status") == "OF").AsDataView().ToTable();

No comments:
Write comments
Recommended Posts × +