Question

In the code-behind I want to apply a dynamic where clause for the entitydatasource, but I want this where to be like and not equal. I have this code working which is equal I want an equivalence code that somehow translates this into a Like statement.

EntityDataSource1.WhereParameters.Add("Name", TypeCode.String, tbxSearch.Text);

Solution after reading Jupaol comment :

Xaml:

<WhereParameters>
   <asp:ControlParameter ControlID="tbxSearch" Name="Name" Type="String" />
</WhereParameters>

Code Behind: (on load event)

if (string.IsNullOrEmpty(tbxSearch.Text))
{
   this.EntityDataSource1.Where = "1=1";  //fetch all data if empty
}
else
{
   this.EntityDataSource1.Where = "it.Name like '%' + @Name + '%'"; //filter
}
Was it helpful?

Solution

In that code you are only adding a parameter, the place where you need to define your like comparison, is in the where clause

The code you posted can be translated into:

    <asp:EntityDataSource runat="server" ID="eds"
        .....
        Where="it.fname like '%' + @Name + '%'"
        <WhereParameters>
            <asp:ControlParameter ControlID="tbxSearch" Name="Name" DefaultValue="" />
        </WhereParameters>

To Add the where in code behind:

this.eds.Where = "it.fname like '%' + @Name + '%'";

Edit1:

For some reason, if I place the parameter declaration like yours (in code), it is not working, however if I place the parameter in the markup like this:

        <WhereParameters>
            <asp:ControlParameter ControlID="tbxSearch" Name="Name" Type="String" DefaultValue="" />
        </WhereParameters>

And in Page_Load

this.eds.Where = "it.fname like '%' + @Name + '%'";

It works

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top