Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 777
GibboK Avatar asked Jan 12 '11 11:01

GibboK


2 Answers

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?

like image 195
naveen Avatar answered Oct 28 '22 21:10

naveen


I have always changed the DefaultValue to set a parameter from code-behind, like so:

uxEntityDataSourceNodes.WhereParameters["SelectedValue"].DefaultValue
    = uxTreeView1.SelectedValue.ToString();

It worked for me.

Edit: You can then specify the WhereParameter in the aspx-File and don't need to add it to the WhereParameters collection in code-behind:

<asp:EntityDataSource ID="uxEntityDataSourceNodes" runat="server" 
    ConnectionString="name=TestHierarchyEntities" 
    DefaultContainerName="TestHierarchyEntities" EnableFlattening="False" 
    EnableUpdate="True" EntitySetName="CmsCategories"
    Where="it.CategoryId = @SelectedValue" 
    EntityTypeFilter="" Select="">
    <WhereParameters>
        <asp:Parameter Name="SelectedValue" Type="Int32" />
    </WhereParameters>
</asp:EntityDataSource>
like image 40
Slauma Avatar answered Oct 28 '22 21:10

Slauma