Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

ASP.NET Data Source Controls

Building Data-Driven ASP.NET Web Forms Apps

Table of Contents

  • ASP.NET Data Source Controls
    • SqlDataSource
    • EntityDataSource
    • ObjectDataSource
  • Accessing ADO.NET Entity Frameworkwith EntityDataSource
    • Working with Editable Controls
    • Master-Details Navigation
  • Model Binding in Web Forms

ASP.NET Data Source Controls

ASP.NET Data Source Controls

  • ASP.NET provides server controls that take care of data binding details
    • Known as data source controls
    • SqlDataSource, EntityDataSource, ObjectDataSource, XmlDataSource,
  • They are an abstraction over the data source
  • Data-bound server controls can be associated to a data source control
    • Through the DataSourceID property

ASP.NET Data Source Controls

  • ASP.NET data source controls connect the data-bound controls with the database / ORM:
  • Typical scenario:

  • SqlDataSource

    • SqlDataSource provides connection to a relational DB (SQL Server, Oracle, OLE DB, …)
    • Data is manipulated by using commands
      • Select, Update, Insert and Delete commands
      • Commands can be either SQL queries or names of stored procedures
    • Data is processed with a DataSet (by default)
      • The DataSourceMode property specifies whether to use DataSet or DataReader
    • Old-fasioned, better use EntityDataSource

    SqlDataSource – Example

    <asp:SqlDataSource ID="DSCategories" runat="server" 
      ConnectionString="<%$ ConnectionStrings:Northwind %>" 
      SelectCommand="SELECT x FROM [Categories]" />
    <asp:GridView ID="GridViewCategories" runat="server" 
      DataSourceID="DSCategories" DataKeyNames="CategoryID"
      AutoGenerateColumns="True" AllowPaging="True"



    • The EntityDataSource
      • Provides data binding in Web applications that use the ADO.NET Entity Framework (EF)
      • Implements CRUD (create, read, update, and delete) operations
        • Against a database mapped through EF
        • On behalf of data-bound controls
      • Supports complex queries, sorting and paging
    • The Entity Data Model Designer in VS is used to create the EF mappings (.edmx)

    EntityDataSource – Example

    • Define the Entity Data Model (.edmx)
    • Put a list control in the form (e.g. ListBox)
    <asp:ListBox ID="ListBoxCustomers" runat="server">

    EntityDataSource – Example (2)

    • Bind the ListBox to the data model
    • Select the new "Entity" option in the dialog box

    EntityDataSource – Example (3)

    • The VS Designer will then display the available Entity Containers

    EntityDataSource – Example (4)

    • ListBox control bound an EntityDataSource:
      ID="EntityDataSourceCustomers" runat="server" 
      EntitySetName="Customers" />
    <asp:ListBox ID="ListBoxCustomers" runat="server"
      DataTextField="CompanyName" Rows="10"
      DataValueField="CustomerID" />


    Master-Detail Navigation

    • Master-details navigation could be implemented through filtering:
    <asp:EntityDataSource ID="DSCustomers" runat="server" 
      EntitySetName="Customers"  />
    <asp:ListBox ID="ListBoxCustomers" runat="server" 
      DataSourceID="EntityDataSourceCustomers"  />
    <asp:EntityDataSource ID="DSOrders" runat="server"
      EntitySetName="Orders" Where="it.CustomerID=@CustID">
          <asp:ControlParameter Name="CustID" Type="String"
            ControlID="ListBoxCustomers" />

    EntityDataSource: Hints

    • To access navigational properties in EntityDataSource use "Include"
    &#60;asp:EntityDataSource ID="DataSourceOrderDetails"
      EntitySetName="Order_Details" Include="Product"
      runat="server" … /&#62;
    • To use strongly-typed binding (ItemType) use EnableFlattening="false"
    &#60;asp:EntityDataSource ID="DataSourceOrderDetails"
     runat="server" EnableFlattening="false" … /&#62;


    Editable EntityDataSource

    <asp:EntityDataSource ID="EntityDataSourceCustomers"
      runat="server" ConnectionString="name=…"
      EnableInsert="True" EnableUpdate="True"
      EnableDelete="True" EntitySetName="Customers">
    <asp:ListView ID="ListViewCustomers" runat="server"

    BindItem and Bind(…)

    • Editable controls require a two-way binding (data field ←→ UI control)
      • Use BindItem.Field for strongly-typed binding instead of Item.Field
      • Use Bind("PropertyPath") for standard binding instead of Eval("PropertyPath")
      Company Name: <asp:TextBox ID="TextBoxCompanyName" 
      runat="server" Text='<%# BindItem.CompanyName %>' />



    • ObjectDataSource enables data-binding of UI control to collection of objects
      • Instead of directly binding to a database
    • Needs a middle-tier business object class
      • A class providing the Select, Update, Insert, Delete methods (CRUD operations)
    • ObjectDataSource properties
      • TypeName – name of the business object class
      • SelectMethod, UpdateMethod,

    ObjectDataSource – Example

    <asp:ObjectDataSource ID="dsProducts"
      runat="server" TypeName="ObjectDataSourceProducts"
      SelectMethod="GetAll" InsertMethod="Insert"
      UpdateMethod="Update" DeleteMethod="Delete">
    <asp:GridView ID="GridViewProducts" runat="server"
      DataSourceID="dsProducts" DataKeyNames="ProductID">
    public class ObjectDataSourceProducts
      public IEnumerable<Product> GetAll() { … }
      public int UpdateProduct(Product p) { … }

    Building File System Explorerwith ObjectDataSource

    • Define a class to get all files from given folder
    • Add GetAllFiles() method to return a collection of FileInfo objects
    public class FileSystemManager
      public static IEnumerable<FileInfo> 
        GetAllFiles(string folder)
        var files = Directory.GetFiles(folder);
        return files;

    Building File System Explorerwith ObjectDataSource (2)

    • Add a ListView control in the form
    • Bind the ListView to the ObjectDataSource

    Building File System Explorerwith ObjectDataSource (3)

    • Next choose your custom class FilesManager
    • And choose the GetAllFiles(…) method as "SELECT Method"

    Building File System Explorerwith ObjectDataSource (4)

    • Click to configure the ListView control
    • Optionally choose layout and style

    Building File System Explorerwith ObjectDataSource (5)

    • Customize the ListView to show only rows you like
    • Add a parameterfor the SELECT method
      <asp:Parameter DefaultValue="C:\WINDOWS"
        Name="rootFolder" Type="String" />

    Building File System Explorerwith ObjectDataSource (6)

    • The result is:


    Other Data Sources

    • LinqDataSource
      • For LINQ-to-SQL ORM mappings (legacy)
    • Hierarchical
      • XmlDataSource
        • Establishes a connection to an XML source of data (files, documents)
        • DataFile, TranformFile, XPath
      • SiteMapDataSource
    • MS Access – AccessDataSource
      • Derives from SqlDataSource

    Model Binding

    CRUD Operations Made Easy

    What is Model Binding?

    • What is "model"?
      • In data-driven applications "models" are classes that hold the data behind the UI controls
    • What is "model binding" in ASP.NET?
      • Bind data controls directly to methods that provide CRUD functionality
      • SelectMethod → returns IQueryable<T>
      • InsertMethod, UpdateMethod, DeleteMethod → edit the model (entity)


    ASP.NET Data Source Controls

    • Questions?
    You can’t perform that action at this time.