LLBLGenProDataSource and Filtering on the fly

Posts   
 
    
TogasPoon
User
Posts: 42
Joined: 09-Feb-2006
# Posted on: 17-May-2006 18:59:40   

Is it possible to have a filter like:

filter.Add(YourEntityFields.Field Mod "%Foo%")

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 17-May-2006 19:43:52   

Yes set the datasource's FilterToUse property to a new predicate expression with that like predicate.

Frans Bouma | Lead developer LLBLGen Pro
TogasPoon
User
Posts: 42
Joined: 09-Feb-2006
# Posted on: 18-May-2006 16:44:59   

That works if I include a dummy Select Parameter.

Here is what I have:


  Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click

    Me.LLBLGenProDataSource1.FilterToUse = FilterPredicate( _
        Me.txtFirstName.Text, _
        Me.txtLastName.Text, _
        Me.chkTermed.Checked)

  End Sub

Shared Function FilterPredicate(ByVal firstName As String, _
      ByVal lastName As String, ByVal showTermed As Boolean) As PredicateExpression
      Dim pe As IPredicateExpression = New PredicateExpression
      Dim find As String

      If firstName <> String.Empty Then
        find = "%" & firstName & "%"
        pe.Add(EmployeeFields.FirstName Mod find)
      End If

      If lastName <> String.Empty Then
        find = "%" & lastName & "%"
        pe.Add(EmployeeFields.LastName Mod find)
      End If

      If Not showTermed Then
        pe.Add(EmployeeFields.TermedYn = False)
      End If

      Return pe

    End Function


And the llblgendatasource code

<cc1:llblgenprodatasource id="LLBLGenProDataSource1" runat="server" datacontainertype="EntityCollection" CacheLocation="Session"
            entitycollectiontypename="MSI.Main.CollectionClasses.EmployeeCollection, MSI.Main" LivePersistence="True" >
            <SelectParameters>
                <asp:ControlParameter ControlID="txtFirstName" Name="dummy" PropertyName="Text"
                    Type="String" />
            </SelectParameters>
        </cc1:llblgenprodatasource>

If I comment out the dummy select parameter then my filter is never applied and I always get the entire employee collection.

With the dummy parameter I can filter and edit like I would expect.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-May-2006 18:05:04   

The initial filter has to be set in the page_load event for the page. Could you try that for me, please?

This also goes for prefetch paths etc. you want to use.

Frans Bouma | Lead developer LLBLGen Pro
TogasPoon
User
Posts: 42
Joined: 09-Feb-2006
# Posted on: 18-May-2006 18:31:46   

That's where I originally put it.

I moved it back just now to be sure.

When the FitlerToUse code is in in the Page_Load event I always receive the entire collection (no filtering is occureing).

If I put in inside a Not(page.ispostback) if statement I get the same results.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 19-May-2006 21:26:45   

Hmm. I have in one of my testpages a filter on customers, and that works always.

Perhaps it's good if you could post some small example how you set it up which fails. It might be a small detail which you have different from what I have which makes it fail. What I've learned in the past 3-4 months now working with this databinding in ASP.NET 2.0 is that if there's anything fragile in this world, this must be it, so if there's one little detail not correct, it goes haywire.

Frans Bouma | Lead developer LLBLGen Pro
Trig
User
Posts: 96
Joined: 09-Jun-2004
# Posted on: 21-May-2006 20:45:59   

I'm experiencing this same issue. When I set the FilterToUse to a new RelationPredicateBucket, the LLBLGenDataSource2 doesn't re-fetch. I'm watching the DB calls, and no call occurs. I'm setting the FIlterToUse in the Page_Load event.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 22-May-2006 18:34:48   

When setting the FilterToUse, the datasource doesn't set its Refetch property to true so no refetch is performed.

It's a bug, as it does raise a changed event, but it doesn't set refetch to true, so the data isn't refetched from the db. Similar to setting prefetch paths, sorter etc. in code.

To work around it for now till the next build is ready: after you've set the FilterToUse property, set Refetch also to true as done in this example:


protected void _filterCustomersButton_Click( object sender, EventArgs e )
{
    string country = _countryTextBox.Text.Trim();
    if( country.Length <= 0 )
    {
        return;
    }

    _countryLabel.Text = country;
    RelationPredicateBucket filter = new RelationPredicateBucket( CustomersFields.Country == country );
    _customersDS.FilterToUse = filter;
    _customersDS.Refetch = true;
}

(edit) fixed in next build simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Rhywun avatar
Rhywun
User
Posts: 44
Joined: 05-Jan-2005
# Posted on: 30-Jun-2006 22:39:56   

I'm having the same problem as TogasPoon getting a filter using LIKE to work. Following this and various other threads, I set the data source's FilterToUse property to a PredicateExpression when the page loads. I also tried with and without the SelectParameter on the data source. And I tried both with and without setting the Refetch property of the data source to true. But no matter what, when I run the code it behaves as if I am not setting the filter at all - I can only filter by the exact value that I enter in the text box rather than using the LIKE predicate as I expected. In the FormView I get the contents of my EmptyDataTemplate instead of the matching records. Any ideas to get this working? Thanks!

Here is the relevant page:

<cc1:LLBLGenProDataSource ID="policyDataSource"
    runat="server" DataContainerType="EntityCollection" EntityCollectionTypeName="QCTrack.CollectionClasses.PolicyCollection, QCTrack">
    <SelectParameters>
        <asp:ControlParameter ControlID="searchValueEdit" DefaultValue="0" Name="PolicyNumber"
            PropertyName="Text" />
    </SelectParameters>
</cc1:LLBLGenProDataSource>

<asp:FormView ID="formView1" runat="server" DataSourceID="policyDataSource"
    DefaultMode="Insert" DataKeyNames="Id" OnItemCommand="formView1_ItemCommand" AllowPaging="True">
    <EditItemTemplate>
    ...
    </EditItemTemplate>
    <InsertItemTemplate>
    ...
    </InsertItemTemplate>
    <EmptyDataTemplate>
        Not found.
    </EmptyDataTemplate>
</asp:FormView>

And the relevant code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    SetFilter();
    searchValueEdit.Focus();
}

private void SetFilter()
{
    if (!String.IsNullOrEmpty(searchValueEdit.Text))
    {
        // Set the filter
        string filter = String.Format("%{0}%", searchValueEdit.Text);
        IPredicateExpression predicateExpression = new PredicateExpression();
        FieldLikePredicate predicate;

        predicate = new FieldLikePredicate(PolicyFields.PolicyNumber, filter);
        predicate.CaseSensitiveCollation = false;
        predicateExpression.Add(predicate);

        predicate = new FieldLikePredicate(PolicyFields.InsuredName, filter);
        predicate.CaseSensitiveCollation = false;
        predicateExpression.AddWithOr(predicate);

        policyDataSource.FilterToUse = predicateExpression;
        policyDataSource.Refetch = true;
    }
}

protected void searchButton_Click(object sender, EventArgs e)
{
    formView1.ChangeMode(FormViewMode.Edit);
}
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 30-Jun-2006 23:12:37   

You set it every time, only set it when the page isn't a postback. Could you try that? Also if you remove the selectparameter filtering, and just use this filter, does that work or not?

Frans Bouma | Lead developer LLBLGen Pro
Rhywun avatar
Rhywun
User
Posts: 44
Joined: 05-Jan-2005
# Posted on: 30-Jun-2006 23:34:07   

Otis wrote:

You set it every time, only set it when the page isn't a postback. Could you try that? Also if you remove the selectparameter filtering, and just use this filter, does that work or not?

Following both suggestions results in the entire collection displaying in the FormView no matter what I enter in the text box.

However, removing the SelectParameter but setting the filter every time, not just on !IsPostback, gives more interesting results: the filter works properly but only on a "second" postback by clicking one of the page numbers in the FormView's footer. That is, I search for "Joe" and get 4 records, which is correct. Then I try to search for "James" and click the Search button, I still get the results for "Joe". Then I click a page number and get the correct results for "James". I've seen this behavior once, a long time ago - forgot how to fix it simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 30-Jun-2006 23:56:11   

haha yeah duh wink , you should reset the filter on the datasource when you re-type the name in the textbox, so you should add a button or something which executes the filter and in the handler of that button you should set the datasource's filter again. (so in your searchbutton_click event handler)

Frans Bouma | Lead developer LLBLGen Pro
Rhywun avatar
Rhywun
User
Posts: 44
Joined: 05-Jan-2005
# Posted on: 13-Jul-2006 20:53:31   

Otis wrote:

haha yeah duh wink , you should reset the filter on the datasource when you re-type the name in the textbox, so you should add a button or something which executes the filter and in the handler of that button you should set the datasource's filter again. (so in your searchbutton_click event handler)

That makes sense... I'm getting MUCH better results now - thanks. And it works without the SelectParameters node, too.

Next question... Is there an easy way to not fetch any data upon first loading the page? I don't want to see the FormView's EmptyDataTemplate either. So far all I can accomplish is either returning the entire collection or filtering by some "unexpected value" and showing the EmptyDataTemplate. Is there a better way?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 13-Jul-2006 21:20:12   

No, I haven't found a better way than specifying at first load of the page a filter which won't result in a succesful fetch. The thing is that the grid will call ExecuteSelect upon first view and as the rest isn't set, the filter is empty and all data is fetched...

Frans Bouma | Lead developer LLBLGen Pro
Rhywun avatar
Rhywun
User
Posts: 44
Joined: 05-Jan-2005
# Posted on: 13-Jul-2006 22:05:02   

Otis wrote:

No, I haven't found a better way than specifying at first load of the page a filter which won't result in a succesful fetch. The thing is that the grid will call ExecuteSelect upon first view and as the rest isn't set, the filter is empty and all data is fetched...

OK, good enough.