Posts Tagged ‘ObjectDataSource’

CodeFluent Entities and ASP.NET Web Forms Binding: Selecting Data

November 16, 2011 3 comments

CodeFluent Entities, using the Business Object Model producer, generates a set of .NET classes (C# or VB.NET) which you can easily use in any .NET application including ASP.NET Web Forms one.

Today’s post is the first of a series I’ll do over the next week that will illustrate how to do common operations creating a hand-made ASP.NET Web Form web site using a CodeFluent Entities generated Business Object Model.

So let’s start with the basics and how to select data. The key control to do that is the ObjectDataSource. In this example, we’re using it to load all contacts of my application:

<asp:ObjectDataSource runat="server" ID="ContactDataSource" SelectMethod="LoadAll" TypeName="ContactAdmin.ContactCollection" DataObjectTypeName="ContactAdmin.Contact" />

As you can see we’re mapping the ObjectDataSource control on the LoadAll method of the ContactCollection class, and indicating that the manipulated data objects are of the Contact type, which is my entity class. Please note that all this was automatically generated from my model, which contains a single entity named Contact.

To display the returned data, let’s use an <asp:gridview> control.  The Gridview below has 3 columns, all of them being standard BoundFields:

<asp:GridView ID="GridView" runat="server" DataSourceID="ContactDataSource" AutoGenerateColumns="False">
        <asp:BoundField HeaderText="Id" DataField="Id" />
        <asp:BoundField HeaderText="First Name" DataField="FirstName" />
        <asp:BoundField HeaderText="Last Name" DataField="LastName" />

As you can see the GridView control uses the ContactDataSource as a data source, and displays the Id, FirstName and LastName properties of my Contact class. Here’s the result: