Click here to Skip to main content
Rate this: bad
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
Posted 14-Mar-13 4:49am
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
Please Sign up or sign in to vote.

Solution 1

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

here i want to replace and 1=1
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 OriginalGriff 335
1 Peter Leow 135
2 Zoltán Zörgő 130
3 Afzaal Ahmad Zeeshan 115
4 DamithSL 100
0 Sergey Alexandrovich Kryukov 7,442
1 OriginalGriff 5,996
2 Peter Leow 4,292
3 CHill60 2,858
4 Mika Wendelius 2,850

Advertise | Privacy | Mobile
Web03 | 2.8.150224.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