Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filter Expression to include NULL

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
0
0
GB
I have the following FilterExpression to search for records in a gridview.

Code:
 FilterExpression="[FirstName] LIKE '{0}%' OR SURNAME LIKE '{1}%'"

This works if I enter information into both fields but wont work if I leave either field blank.

I have set the filter parameters to convertemptystringtonull = true

Thanks
 
null is different than an empty string so you need to check for null
if the value is null ignore the criteria altogether.
Code:
var filter = "";
if(firstname != null && lastname != null)
{
  filter = "[FirstName] LIKE '{0}%' OR SURNAME LIKE '{1}%'" ;
}
else if(firstname != null)
{
   filter = "[FirstName] LIKE '{0}%'" ;
}
else if(lastname != null)
{
  filter = "SURNAME LIKE '{1}%'" ;
}
return string.Format(filter, firstname, lastname);

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Will your allow for empty fields? I want the user to be able to leave fields empty and return records on the specified information
 
read the code :) the snippet above will only filter if the value is not null. this assumes the value will never be an empty string. otherwise replace [tt]!= null[/tt] with [tt]string.IsNullOrEmpty(value) == false[/tt]

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top