How To: Access Underlying Data of the ObjectDataSource

A gentleman in my ASP.NET 2.0 class a while ago asked me how to access the underlying object used for the ObjectDataSource control.  Suppose you have some code in App_Code like so:

public class DataManager
public DataSet SelectDataSet()
// code that populates the dataset...
DataSet ds GetPopulatedDataSet();
// supporting methods here...

Now on the .aspx page we have an ObjectDataSource control as follows:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 

Notice the OnSelected attribute points to a method called ObjectDataSource1_Selected. This is the event where we can get access to the underlying data object. Here is the code for the event handler:

protected void ObjectDataSource1_Selected
object sender, ObjectDataSourceStatusEventArgs e)
          DataSet underlyingDataSet 
e.ReturnValue as DataSet;
// use the underlyDataSet...

The key to getting access to the underlying object is calling the ReturnValue off of the ObjectDataSourceStatusEventArgs object. ReturnValue is typed as Object, so it is necessary to cast to what the underlying data-type is. In the above example, the underlying data-type is a DataSet.


Comments have been disabled for this content.