asp.net - How can I correctly write this database search query? -


i'm attempting write query search database based on many different form fields. want query filter fields entered , disregard fields not entered on form. example:

form fields: -project name -project number -project manager

so if user enters "joe" project name, should return results form of "joe" in name field regardless of other values in record.

i've been trying accomplish strictly sql , i'm having incorrect (yet expected) results. i've got pretty hefty queries, , apologize if they're pretty far fetched.

this first query set using , statements conditions. realized doesn't work, null values in database still don't picked wildcards.

    <listdatasource commandtext=" select [surveyid],         [projectnumber],         [siteno],         [person],         [subatpdate],         [projectname],         [priorsurveyor],         [siteaddress],         [sitecity],         [sitestate],         [sitezip],         [survey],         [zoning],         [environmental],         [emg_projectnumber],         [zoning_projectnumber],         [surveyor_projectnumber]    tblrawprojectdatabase   [projectnumber] '%' + @projectnumber + '%'         , [siteno] '%' + @siteno + '%'         , [person] '%' + @person + '%'         , [subatpdate] '%' + @subatpdate + '%'         , [projectname] '%' + @projectname + '%'         , [priorsurveyor] '%' + @priorsurveyor + '%'         , [siteaddress] '%' + @siteaddress + '%'         , [sitecity] '%' + @sitecity + '%'         , [sitestate] '%' + @sitestate + '%'         , [sitezip] '%' + @sitezip + '%'         , [survey] '%' + @survey + '%'         , [zoning] '%' + @zoning + '%'         , [environmental] '%' + @environmental + '%'         , [emg_projectnumber] '%' + @emg_projectnumber + '%'         , [zoning_projectnumber] '%' + @zoning_projectnumber + '%'         , [surveyor_projectnumber] '%' + @surveyor_projectnumber + '%' " 

my second attempt checks null values. works little better, except if enter "jim" project manager, returns records null project manager value.

       <listdatasource commandtext=" select [surveyid],         [projectnumber],         [siteno],         [person],         [subatpdate],         [projectname],         [priorsurveyor],         [siteaddress],         [sitecity],         [sitestate],         [sitezip],         [survey],         [zoning],         [environmental],         [emg_projectnumber],         [zoning_projectnumber],         [surveyor_projectnumber]    tblrawprojectdatabase   ( [projectnumber] '%' + @projectnumber + '%'            or [projectnumber] null )         , ( [siteno] '%' + @siteno + '%'                or [siteno] null )         , ( [person] '%' + @person + '%'                or [person] null )         , ( [priorsurveyor] '%' + @priorsurveyor + '%'                or [priorsurveyor] null )         , ( [siteaddress] '%' + @siteaddress + '%'                or [siteaddress] null )         , ( [sitecity] '%' + @sitecity + '%'                or [sitecity] null )         , ( [sitestate] '%' + @sitestate + '%'                or [sitestate] null )         , ( [sitezip] '%' + @sitezip + '%'                or [sitezip] null )         , ( [survey] '%' + @survey + '%'                or [survey] null )         , ( [zoning] '%' + @zoning + '%'                or [zoning] null )         , ( [environmental] '%' + @environmental + '%'                or [environmental] null )         , ( [emg_projectnumber] '%' + @emg_projectnumber + '%'                or [emg_projectnumber] null )         , ( [zoning_projectnumber] '%' + @zoning_projectnumber + '%'                or [zoning_projectnumber] null )         , ( [surveyor_projectnumber] '%' + @surveyor_projectnumber + '%'                or [surveyor_projectnumber] null ) " 

is there someway use or , make work short circuit evaluation can't check nulls if first condition met?

thank you, recommendations on how accomplish search query excellent.

update:

some valuable information left out in initial post fact i'm using dotnetnuke module called xmodpro. not aware of way use conditional , looping logic need build query asp module restricts using tags (which far see, provides if , select statements , makes variable use more complex should be)

do conditional logic in asp code build commandtext variable. example, if nothing entered in projectname text box, not include in query. also, might consider using or logic instead of and. depend on trying achieve.

here rough idea of how it. it's not real code, shows approach:

string whereclause = "where 1 = 2 " if project name specified. whereclause = whereclause + project name. etc 

also, remember include query parameters.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -