Click here to Skip to main content
11,577,710 members (62,021 online)
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 3:49am
bunzitop1.1K
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)

  Print Answers RSS
0 Peter Leow 180
1 Richard MacCutchan 175
2 DamithSL 150
3 OriginalGriff 135
4 Afzaal Ahmad Zeeshan 95
0 OriginalGriff 830
1 Sergey Alexandrovich Kryukov 701
2 Abhinav S 573
3 F-ES Sitecore 420
4 Dave Kreskowiak 374


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2015
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