Friday, January 27, 2006
« Determine if ASP.NET Page is running in ... | Main | Express Database Editions »

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)

    {

        // ...

    }

}

kick it on DotNetKicks.com   Friday, January 27, 2006 3:54:10 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [5]  | 
Saturday, January 28, 2006 8:56:05 AM (Pacific Standard Time, UTC-08:00)
There is also a e.HandledError property that you can set to true if you want to avoid bubbling the exception further.

Hope this helps,

Scott
Tuesday, January 31, 2006 8:31:49 AM (Pacific Standard Time, UTC-08:00)
Scott,

Nice tidbit of info. Thanks!
Andrew Robinson
Thursday, March 16, 2006 8:13:20 AM (Pacific Standard Time, UTC-08:00)
I see where your Object Data Source requires a DataTable. I'm not sure off the top of my head how the SQLDataSource works since it obfiscates so much of the heavy lifting. Do you know if your ODS works with SQLDataSource?
matt
Thursday, March 16, 2006 8:17:16 AM (Pacific Standard Time, UTC-08:00)
sorry, i read that wrong. the ODS returns a datatable. I assume then that it should work with the SQLDataSource. I should know soon.
matt
Thursday, March 16, 2006 8:23:35 AM (Pacific Standard Time, UTC-08:00)
The ODS and the SDS look pretty much the same from the Grid View's point of view. The ODS maintains the plumbing to a data object while the SDSwill plumb up a direct SQL connection (Select, Insert, Update, Delete).

If you want to use the SDS, it should work just fine with my example. Maybe I should write up a similar posting but in general, I never use SDS. They tent to blur the line between presentation and data tiers. That might be ok for simple maintenance applications but in the bigger picture, I don't care for it.
Andrew Robinson
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview