Results 1 to 3 of 3

Thread: [RESOLVED] How to merge BindingSource filters?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Resolved [RESOLVED] How to merge BindingSource filters?

    I am trying to merge two filters but it aint happening:

    Code:
    ClientProfileBindingSource.Filter = "ClientType = '" & "Seeker" & "'" And "ClientType = '" & "Both" & "'"
    error:

    Conversion from string "ClientType = 'Seeker'" to type 'Long' is not valid.
    please help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How to merge BindingSource filters?

    The problem with your code is that you have the 'And' OUTSIDE the literal string instead of inside. As such it's being treated as a logical operator with those two strings as arguments instead of as part of the string. You can change your code form this:
    vb.net Code:
    1. ClientProfileBindingSource.Filter = "ClientType = '" & "Seeker" & "'" And "ClientType = '" & "Both" & "'"
    to this:
    vb.net Code:
    1. ClientProfileBindingSource.Filter = "ClientType = '" & "Seeker" & "' AND ClientType = '" & "Both" & "'"
    and it will work.

    That said, if that is your real code then all the & operators are unnecessary. You're just joining multiple literal strings so all you need is a single literal string:
    vb.net Code:
    1. ClientProfileBindingSource.Filter = "ClientType = 'Seeker' AND ClientType = 'Both'"
    I'll assume that your real code isn't like that but rather you are actually using variables for the two values. In that case, this is a perfect candidiate for the String.Format method:
    vb.net Code:
    1. ClientProfileBindingSource.Filter = String.Format("ClientType = '{0}' AND ClientType = '{1}'", "Seeker", "Both")
    You would use your variables in place of the last two arguments. Isn't that clearer?

    Finally, how can the same ClientType value be equal to "Seeker" and "Both" at the same time? Should you maybe be using OR rather than AND?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: How to merge BindingSource filters?

    Thank you Sir, Your last statement did the trick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width