Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
here i have multiple check boxes and when i check one of it i get the data retrieved from database for the particular checked value...
But here i need the provision to add multiple check box values into the select command dynamically whenever certain diff combination of check boxes are checked
Posted
Comments
BillW33 10-Aug-12 8:53am    
That's nice. Exactly what is your question? Can you show us the code for what you have tried so far?
[no name] 10-Aug-12 9:01am    
Well you go ahead and do that.
Sergey Alexandrovich Kryukov 10-Aug-12 15:48pm    
Not a question. CheckBox in what? Exact type of CheckBox, please.
--SA

I am not a C# developer, so can't provide you the exact code, but possible logic for achieving this will be -

say for example, you have a master list of all possible checkbox values
Now, rather then you are retrieving value from database everytime a value is checked, this should be triggered through a button (form submit) which will pass all of the checked values.

In the backend, you retrieve all these checked value and loop it for generating dynamic query string. Something like below -
ObjectA = {array of master checkbox values}
ObjectB = {array of master checkbox value columns for preparing query}
ObjectC = {array of all checked values submitted}
StringQuery = "select x,y,z from mytable where ";

for(int i = 0; i < ObjectA.size(); i++) {
  for(int j = 0; j < ObjectC.size(); j++) {
    if(ObjectA[i] == ObjectC[j]) {
      StringQuery += " " + ObjectB[i] + " = " + ObjectC[j];
      break;
    }
  }
  if(i+1 > ObjectA.size()) {
    StringQuery += " AND ";
  }
}

Many thanks,
Niral Soni
 
Share this answer
 
Comments
Volynsky Alex 10-Aug-12 10:09am    
Good answer
+5!
Sergey Alexandrovich Kryukov 10-Aug-12 15:47pm    
Please see me comment below explaining why I don't thing so. The flaw it too critical to ignore.
--SA
Sergey Alexandrovich Kryukov 10-Aug-12 15:46pm    
I need to explain why this solution is very bad and should never be used. This is an open door to SQL Injection exploit:
http://en.wikipedia.org/wiki/SQL_injection

Please see the article, it explains the solution: parametrized query.

Here is what you do: obtain the query through concatenation. The concatenated "Object" strings are obtained from UI, and it can contain anything, including... right, a fragment of SQL code, to screw up things.

Sorry, I have to vote 1 for this answer.
--SA
Way 1:Put a button on your UI and in the button click event write the query.

Way 2:In every check box made auto post back true and in the query use IN operator for checking. so that no of time you check the check box it will show different different result.

I will prefer upper one.
 
Share this answer
 

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