Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

a better way for user searches

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan Holmes - 02 Jan 2008 22:31 GMT
i have a table of products and there is a screen to search for them.  I
am stuck as to how to distinguish that the user doesn't want to include
that criteria with that criteria is blank.

Here is a sample:

public List<ProductInfo> List(IVS.Framework.Identity id, ProductInfo
productInfo1, ProductInfo productInfo2)
{
  List<ProductInfo> returnData = null;
  StringBuilder sql = new StringBuilder();

...snipped stuff

 if (!string.IsNullOrEmpty(productInfo1.ProductID))
   sql.AppendFormat(" AND ProductID LIKE '{0}%'", productInfo1.ProductID);

 if (!string.IsNullOrEmpty(productInfo1.CategoryID))
   sql.AppendFormat(" AND CategoryID LIKE '{0}%'",
productInfo1.CategoryID);
 else if (productInfo1.CategoryID == null)
   sql.Append(" AND CategoryID IS NULL");

in the case of a productid it can never be null but a category could.
How have you guys done this?  The above will always look for categoryid
of null even when i only want to search by productid or i have to
include a category too.

dan
Marc Gravell - 02 Jan 2008 23:27 GMT
First off: *never* concatenate user input; use parameters instead...
i.e. "AND ProductID LIKE @ProductID"
(and add a parameter named @ProductID with Value =
productInfo1.ProductID + "%".

However; back to the question ;-p Various options:
* A checkbox next to each that checks itself automatically when a
value is entered, but can be checked manually for searching blank
* Radios for value / blank: (o) [Textbox] (o) (blank) - defaults to
value obviously (and selecting (blank) would disable the Textbox)
* Drop-down list of available options, with (blank) at the top?
* A more dynamic search UI - i.e. user adds criteria by selecting a
property (from a list) *and* the value - and can and multiple rows -
i.e.

[Name] = Fred
[Order Ref] =
[Foo] = bar
[Select option] = {value}

(the bottom option adds a new row; the Order Ref is searching for a
blank)
This approach also: handles large numbers of searchable fields without
needing a massive search form; allows for more complex "(this and
that) or (theother)" searches; makes it easier to fit operator options
(equals/starts-with/less-than/more-than etc) without making the UI too
ugly (OK; still ugly, but not too much so).

Of course, the alternative would be to go all "google", with a single
text-box and simply parse the tokens - i.e.:
  Name=Fred and Order Ref = "" and Foo=bar
(or something)

Marc
Dan Holmes - 03 Jan 2008 13:36 GMT
> First off: *never* concatenate user input; use parameters instead...
> i.e. "AND ProductID LIKE @ProductID"
> (and add a parameter named @ProductID with Value =
> productInfo1.ProductID + "%".

thanks i missed that in this one.  My other code does that but i looked
right over it here.

> However; back to the question ;-p Various options:
> * A checkbox next to each that checks itself automatically when a
[quoted text clipped - 25 lines]
>
> Marc

what data structure you use for holding the search criteria with this
idea?  I guess i could create one...perhaps something like

class Criteria
{
    public string columnname;
    public string value;
    public Operation operator;
}
Enum Operation
{
    Equals
    , GreaterThan
    , LessThan
    ...
}
Marc Gravell - 03 Jan 2008 20:33 GMT
> perhaps something like
Depending on your actual requirements, yes; I have used something very
similar... probably not public fields, though ;-p

Marc
Marc Gravell - 03 Jan 2008 20:39 GMT
Actually - seeing the later posts, I'd agree that Expression is a
better way to encapsulate this...

Plus (as has already been pointed out) it would allow the system to
write the SQL for you.

For more info on building the query, see:
http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_t
hread/thread/76353240ec0652c3/4e2cc66c3d39ee9f


Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.