Brent Gardner's Blog

It's all about the code.

Syndication

Tags

    No tags have been created or used yet.

Navigation

ASP.NET Bindable DataSet DataSource

O/R mappers have gotten allot of press lately, and for good reason. They take away much of the busy work of building a data access layer (DAL). They work fantastically for smaller projects, and as of Visual Studio 2007, they will receive true legitimacy with support from Microsoft (via LINQ-to-SQL). Occasionally I run into various limitations however, such as the inability to perform advanced operations like recursing relationships. Fortunately, Object Models have a more advanced, if not slightly trickier to implement cousin: Typed-Datasets.

Typed-Datasets have the features of a DataSet (run-time parsable schema, constraint enforcement, xml exportability), as well as the features that make O/R generated object models handy (compile-time parsable schema, i.e. intellisense). Unfortunately DataSets haven't received as much attention in databinding as have other object models. For one thing, they don't have an associated DataSource. The ObjectDataSource doesn't work with them for many reasons, least of which because DataRow's don't have a default constructor. The SqlDataSource is great, but it accesses the database directly, making a middle tier impossible.

Not wanting to see DataSets left feeling isolated and unwanted, I've created a bindable DataSource for them, thanks in part to example code in a blog post by Manuel Abadia. There are just a few things that bear explaining first. Unlike most O/R mapper code, the DsDataSource is designed for pre-loading (vs. lazy loading). The idea being to bind all DsDataSources on the page to different tables within a single DataSet, and to populate that DataSet completely and immediately upon page load:

Partial Class _Default

    Inherits System.Web.UI.Page

    ' First, we create a new dataset attached to the page instance:

    Protected pDs As New ExampleDs

    ' Right off the bat, we want to populate the DataSet from the DB:

    Protected Sub Page_Load() Handles Me.Load

        ExampleDs.EmployeeDataTable.LoadAll(Ds)

    End Sub

    ' Lastly, we need to expose the DataSet as a property

    ’so that the DsDataSource controls can access it:

    Public ReadOnly Property Ds() As ExampleDs

        Get

            Return pDs

        End Get

    End Property

End Class

The .aspx code looks like this:

<ow:DsDataSource ID="dsEmployee" runat="server"

<!—Property from .vb code -->

DataSetProperty="Ds" TableName="Employee"

<!-- TypeName of the DataSet --> 

TypeName="ExampleDs"

<!-- TypeName of the DataTable --> 

DataObjectTypeName="ExampleDs+Employee" />

<!—- Then, just set the DataSourceId on the GridView: -->

<asp:GridView runat="server" ID="gvEmployee" DataSourceID="dsEmployee"

AutoGenerateColumns="false" DataKeyNames="EmployeeId">

<Columns>…</Columns></asp:GridView>

With no SelectMethod, as shown, the DsDataSource will display all rows in the target table. If necessary, you can specify a SelectMethod to return a subset of rows. The InsertMethod and UpdateMethod have been rolled into a common "Persist" method, and the obligatory DeleteMethod property is available as well. Aside from these minor differences, the DsDataSource functions much like the ObjectDataSource. It even attempts to use reflection to set the row properties if available, so that you can put business logic in your typed DataRow classes.

Hopefully that will be enough to get you coding, and I have an example available for download as well.

Happy coding!

- Brent

 

Published Thursday, April 12, 2007 9:01 PM by Brent


Attachment(s): DsDataSource.zip

Comments

No Comments

Anonymous comments are disabled