Click here to Skip to main content
15,886,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a query such as
commandtext = "SELECT * FROM TABLENAME WHERE 1=1 and 1=1 and 1=1


i have two combobox and i want to filter data according to the value available in the combobox.

what i want is
if i choose combobox 1 then i want to the code to be
commandtext = SELECT * FROM TABLENAME WHERE 1=1 and columnvalue = combobox value and 1=1


i want to replace the conditions (and 1=1) during execution of program

please help me

Regards
Bunzitop
Posted
Comments
[no name] 14-Mar-13 9:55am    
Based on the very vague description of your problem, I would suggest a redesign of your code to avoid this situation.

1 solution

So do this:

commandtext = "Select * From TableName Where columnvalue = '" + combobox.value + "'";
 
Share this answer
 
Comments
bunzitop 14-Mar-13 10:02am    
i am currently using the code you suggested as
commandtext = "Select * From TableName Where columnvalue = '" + combobox.value + "'";

but my requirement is to filter the database according to the value of combobox.
if i have 4 combobox then it will have 16 possibilites to filter the database

but if i wrote the original code as
commandstring = "SELECT * FROM TABLENAME WHERE 1=1 and 1="

replaced the value "and 1=1 " with "and value = '"+combobox.value+"' "
then i will be more dynamic
sp i dont have to check each combobox if it is blank or not..
Richard C Bishop 14-Mar-13 10:05am    
You will have to check each combobox or else you won't know how many filters to use. You might want to use case statements if you have a lot of potential combinations.
bunzitop 14-Mar-13 10:10am    
i want to check as


if(combobox1.text!="")
{
here i want to replace and 1=1
}
if(combobox2.text!="")
{
here also i want to replace second 1=1 with "and value= '"+combobox.value+"'
}


simiilarly for others
finally '
my command will be
SE:ECT * FROM TABLENAME WHERE 1=1 +(and followed by ONLY the selected values of select combobox )
bunzitop 14-Mar-13 10:12am    
can u tell me how to append condition in existing sqlcommand

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