Search code examples
c#asp.netframeentitydatasource

Filtering data using EntityDataSource and WHERE


Hi I have an EntityDataSource.

I need programmatically SEND a variable (@SelectedValue) to be used in a WHERE Filter for the EntityDataSource .

Can you post a simple core to show me how to do it? Thanks for your time!

To create WhereParameters on EntityDataSource I use this code:

            Parameter parameter = new Parameter("SelectedValue", TypeCode.Int32, uxTreeView1.SelectedValue);
            parameter.DefaultValue = "0";
            uxEntityDataSourceNodes.WhereParameters.Add(parameter);`

Here the code for the Control:

        <asp:EntityDataSource ID="uxEntityDataSourceNodes" runat="server" 
        ConnectionString="name=TestHierarchyEntities" 
        DefaultContainerName="TestHierarchyEntities" EnableFlattening="False" 
        EnableUpdate="True" EntitySetName="CmsCategories" Where="it.CategoryId = @SelectedValue" 
        EntityTypeFilter="" Select="">
    </asp:EntityDataSource>

Solution

  • Read this?

    The Entity Framework and ASP.NET - Filtering, Ordering, and Grouping Data


    Update: An example with Northwind Products and Categories Table.
    DropDownList lists the Categories and the GridView displays the Products filtered by Category.

    The ASPX

    <asp:DropDownList ID="uxTreeView1" runat="server" 
                AutoPostBack="true"
                AppendDataBoundItems="true"
                DataSourceID="EntityDataSource1" 
                DataTextField="CategoryName" 
                DataValueField="CategoryID" 
                OnSelectedIndexChanged="uxTreeView1_SelectedIndexChanged">
        <asp:ListItem Text="Select Category" Value="0"></asp:ListItem>
    </asp:DropDownList>
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
        ConnectionString="name=NorthwindEntities" 
        DefaultContainerName="NorthwindEntities" EnableFlattening="False" 
        EntitySetName="Categories" Select="it.[CategoryID], it.[CategoryName]">
    </asp:EntityDataSource>
    <asp:GridView ID="GridView1" runat="server" 
                AutoGenerateColumns="False" 
                DataSourceID="EntityDataSource2"
                DataKeyNames="ProductID">
        <Columns>
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                ReadOnly="True" SortExpression="ProductName" />
            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                ReadOnly="True" SortExpression="CategoryID" />
        </Columns>
    </asp:GridView>
    <asp:EntityDataSource ID="EntityDataSource2" runat="server" 
        ConnectionString="name=NorthwindEntities" 
        DefaultContainerName="NorthwindEntities" EnableFlattening="False" 
        EntitySetName="Products" 
        Select="it.[ProductID], it.[ProductName], it.[CategoryID]">
    </asp:EntityDataSource>
    

    The ASPX.CS

    protected void uxTreeView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        EntityDataSource2.WhereParameters.Clear();
        EntityDataSource2.AutoGenerateWhereClause = true;
        //alternatively
        //EntityDataSource2.Where = "it.[CategoryID] = @CategoryID";
        EntityDataSource2.WhereParameters.Add("CategoryID", TypeCode.Int32, uxTreeView1.SelectedValue);
    }
    

    Is this what you are looking for?