This is one that has been driving me crazy for a few weeks. I finally decided to get to the root of it:
I have an ODS control that sits between a GridView and a data class that returns a DataTable. I manipulate the datatable in the ObjectDataSource_Selected event. If my data class throws an exception, I never see it, however I receive a "System.NullReferenceException: Object reference not set to an instance of an object." when attempting to access the Rows.Count property within the ODS Selected event:
protected void ObjectDataSourceMain_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
DataTable dataTable = (DataTable)e.ReturnValue;
if (dataTable.Rows.Count == 0) // <- Exception!
{
// ...
}
}
The solution involves checking the e.Exception property and throwing the exception when e.Exception is not null. In this case I receive the offending "System.Data.SqlClient.SqlException: Invalid column name 'CcontentID'." exception:
protected void ObjectDataSourceMain_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
throw e.Exception;
}
DataTable dataTable = (DataTable)e.ReturnValue;
if (dataTable.Rows.Count == 0)
{
// ...
}
}