Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005
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 14-Mar-13 4:49am
Comments
ThePhantomUpvoter at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

So do this:
 
 commandtext = "Select * From TableName Where columnvalue = '" + combobox.value + "'";
  Permalink  
Comments
bunzitop at 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..
richcb at 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 at 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 at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100