Getting "Object reference not set to an instance of an object" when searching a datatable

Friday, July 4, 2014

I keep getting

Object reference not set to an instance of an object

exceptions when searching the DataTable even though I believe I have initialized the datatable.

I am trying to do a case insensitive search of the data table filtered for currentqueryvariable.

When I looked at the different data tables through a breakpoint right before the if/else, all the datasets and datatable were fully populated.

Thanks for any help.

DataSet ds = MyData;
DataTable dt = ds.Tables[tableName];
DataTable filtered = new DataTable();
filtered = dt;

if (currentqueryvariable != "")
IEnumerable<DataRow> rows =
filtered.AsEnumerable().Where(r => r.Field<string>("question").ToUpper().Contains(currentqueryvariable.ToUpper())
|| r.Field<string>("options").ToUpper().Contains(currentqueryvariable.ToUpper())
|| r.Field<string>("explanation").ToUpper().Contains(currentqueryvariable.ToUpper())
|| r.Field<string>("Source").ToUpper().Contains(currentqueryvariable.ToUpper()));

filtered = rows.CopyToDataTable();