Home > .net 2.0, c#, Visual Studio 2005, Visual Studio 2008 > Connecting an ObjectDataSource to a DataTable in Code-behind

Connecting an ObjectDataSource to a DataTable in Code-behind

June 6, 2007

After Googling for quite a while, I never managed to find a good step-by-step or MSDN article on creating ObjectDataSources programmatically and then linking them to DataTables, GridViews, etc.  So, after disecting the UI-layer code, here’s what I came up with.

When placing a server-side control on an ASPX page, your code looks similar to:

<asp:ObjectDataSource ID=”odsLookupItemList” runat=”server”

SelectMethod=”GetItemCollectionByFirstNameLastName”

TypeName=”Framework.Collections”>

<SelectParameters>

<asp:ControlParameter ControlID=”txtFirstName”

Name=”firstName” PropertyName=”Text” Type=”String” />

<asp:ControlParameter ControlID=”txtLastName”

Name=”lastName” PropertyName=”Text” Type=”String” />

</SelectParameters>

</asp:ObjectDataSource>

That’s fine and dandy; however, sometimes you don’t want the ODS to fire off on Page_Load, but after an event has occured on the page, such as, in this case, searching for an Item object by first or last name.

ObjectDataSource odsLookupItemList = new ObjectDataSource();

odsLookupItemList.SelectMethod = “GetItemCollectionByFirstOrLastName”;

odsLookupItemList.TypeName = “Framework.Collections”;

odsLookupItemList.SelectParameters.Add(“firstName”, TypeCode.String, txtFirstName.Text);

odsLookupItemList.SelectParameters.Add(“lastName”, TypeCode.String, txtLastName.Text);

DataTable dt = (DataTable)odsLookupItemList.Select();

You can also shortcut, when creating the ODS object, to save a few lines of code, by placing the TypeName and Select Method into an overloaded constructor:

ObjectDataSource ods = new ObjectDataSource(“Framework.Collections”, “GetItemCollectionByFirstOrLastName”);

This requires your ODS to return a result that is either convertable to the object you are trying to place it in (such as a DataTable) or, even better, placing it back in it’s original object type (without casting) and iterating through as necessary for your application.

Now, rather than firing off (and, if the SelectParameters are null, producing nothing), the actual ObjectDataSource isn’t even created until it’s necessary and it’s event, such as a Button’s Click event, has occured.

Tags: ,

  1. Craig
    November 7, 2007 at 5:31 pm

    Can you tell me what the odsLookupParent is. To me it seems like its another objectdatasource to make the call, but your loading the odsLookupItemList. I’m confused.

  2. November 8, 2007 at 2:32 pm

    @Craig-

    You’re totally correct–those are typos in the code. Usually I try to “generic” my code when I post it up. The actual object I was working with was for a report card application for parents, so the object was odsLookupParent. I renamed the code to odsLookupItemList for the blog. It’s now corrected; sorry for the confusion.🙂

  3. Vinny
    December 17, 2008 at 7:55 pm

    What do i set the DatasourceID to on the aspx page? i get the error “The DataSourceID of ‘grdvwPlacements’ must be the ID of a control of type IDataSource. A control with ID ‘placementOBDS’ could not be found.” this is my codebehind

    if (e.CommandName == “GetPlacements”)
    {
    GridViewRow selectedRow = (GridViewRow) (((Control) e.CommandSource).NamingContainer);
    ObjectDataSource placementOBDS = new ObjectDataSource();
    placementOBDS.TypeName = “Lists”;
    placementOBDS.SelectMethod = “GetPlacements”;
    placementOBDS.SelectParameters.Add(“CandidateID”, grdvwCandidates.DataKeys [selectedRow.RowIndex].Values [0].ToString());
    placementOBDS.Select();
    grdvwPlacements.DataSourceID = “placementOBDS”;
    }
    Thanks!!

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: