LinqDataSource – Visual Studio 2008 Beta 2

One of the new features within Visual Studio 2008 is the linqDataSource for ASP.net applications.

This data source allows you to attach directly to a DataContext allowing you to access Linq objects without writing any code.  Because it follows the DataSource interface, existing controls such as the GridView can connect and display data based on it.

The DataSource also handles mappings form GridView requests to the database. For example, Sorting is done server side, querying the database with ORDER BY, while paging using RowNumber feature of SQL Server 2005. 

One note, the Insert, Update and Delete only appears to work if you go via the DataContext and SELECT * on the object. Not sure if this is by design or a bug, i’ve posted a note on the forum asking.

Using the data source is very simple.  In fact, if you have used one of the existing data sources then its just the same but instead of pointing to a database, you point to the DataContext.  If you haven’t, in the Toolbox flyout, under Data you will see the LinqDataSource control.  Drag this onto your form, click the smart tag and you will be able to config the options.  If your DataContext isn’t in the list, you need to compile the application for it to be detectable. You can then drag on a GridView control and point it’s data source to the Data Source you just created.

Sample code which turns 4 columns from the Northwind.Customer table:

    AllowSorting=”True” AutoGenerateColumns=”False” DataSourceID=”LinqDataSource1″>
   
                    SortExpression=”CustomerID”>
                    ReadOnly=”True” SortExpression=”CompanyName”>
                    SortExpression=”Phone”>
                    SortExpression=”PostalCode”>
   

    ContextTypeName=”WebPOC.NorthwindDataContext”
    Select=”new (CustomerID, CompanyName, Phone, PostalCode)” TableName=”Customers”>

Simple and effective control, nice addition to the framework.

 

Technorati Tags: , , ,

2 thoughts on “LinqDataSource – Visual Studio 2008 Beta 2”

  1. Does anyone know how to dis-associate the linq datasource ID from the gridview at runtime and use datasource.

Leave a Reply

Your email address will not be published. Required fields are marked *