Click here to Skip to main content
13,089,386 members (58,697 online)
Rate this:
Please Sign up or sign in to vote.
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 3:49am
ThePhantomUpvoter 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 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 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

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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2017
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