Click here to Skip to main content
15,898,991 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have five text boxes - a,b,c,d,e abd a search button

Now the user can provide input in any of the text boxes (either in a or b or c or abc or any possible combinations)

there is a possibility of few textboxes being blank. So a empty ( "" ) string goes into the where clause.

I want a select query in such a way that it should search the data in database according to the values entered by the user, and ignore the the values not entered

select * from tbl_Results where ...

what should b there after where()

thanks in advance
Posted

Check the code Below

C#
private void PrepareSql()
        {
            string sqlstring = "select * from tblResults where 1=1 ";
            if (!txta.equals(""))
                sqlstring += "and column1 like '% " + txta.text + "%' ";
            if (!txtb.equals(""))
                sqlstring += "and column2 like '% " + txtb.text + "%' ";
            if (!txtc.equals(""))
                sqlstring += "and column4 like '% " + txtc.text + "%' ";
            if (!txtd.equals(""))
                sqlstring += "and column5 like '% " + txtd.text + "%' ";

        }

Thanks
--RA
 
Share this answer
 
Hi Maverick,

You can do one thing
SQL
string query="select * from tblResults ";
string sql=string.Empty;
if(!txta.equals(""))     
        sql+="where column1 like '% " + txta.text + "%' ";    


SQL
if(!txtb.equals(""))
     if(sql.equals(""))
        sql+="where column2 like '% " + txtb.text + "%' ";
     else
        sql+="and column2 like '% " + txtb.text + "%' ";


SQL
if(!txtc.equals(""))
     if(sql.equals(""))
        sql+="where column3 like '% " + txtc.text + "%' ";
     else
        sql+="and column3 like '% " + txtc.text + "%' ";


SQL
if(!txtd.equals(""))
     if(sql.equals(""))
        sql+="where column4 like '% " + txtd.text + "%' ";
     else
        sql+="and columnd like '% " + txtd.text + "%' ";


SQL
query +=sql;


Then run the query in "query" variable

Please make it resolved if it resolves your issue

Thanks and Regards
Suman Zalodiya
 
Share this answer
 
v2
Comments
maverick12131 24-Jun-13 15:18pm    
thanks a ton :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900