473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How can I phrase this SQL command?

I am having trouble with writing this SQL command. Basically, I have 4 checkboxes on my form and I want to populate a DataGridView depending on how the checkboxes are ticked. The user can have 0, 1, 2, 3 or all 4 of the checkboxes ticked, meaning that I have 15 different commands to write (if my maths is correct).

For example...

Expand|Select|Wrap|Line Numbers
  1. if(ocrChecked and aqaChecked)
  2. {
  3. sqlCommand = "...";
  4. }
or...

Expand|Select|Wrap|Line Numbers
  1. if(ocrChecked and edexcelChecked)
  2. {
  3. sqlCommand = "...";
  4. }
.

If you don't understand what I mean then I will try to explain it better, but in the mean time if anyone has any suggestions on how I could do this without having to do it inefficiently then please let me know.

By inefficient, I mean that I would have to do...
Expand|Select|Wrap|Line Numbers
  1. if(a b c d)
  2. if(a b c)
  3. if(a b d)
  4. if(a d c)
  5. if(b c d)
  6. if(a b)
  7. if(a c)
  8. if(a d)
  9. if(b c)
  10. if(b d)
  11. if(c d)
  12. if(a)
  13. if(b)
  14. if(c)
  15. if(d)
Thanks!
Apr 20 '11 #1

✓ answered by hype261

In these situations I just dynamically create the SQL command to execute. So for 4 check boxes you would do something like this.

Expand|Select|Wrap|Line Numbers
  1. string sql = "Select * FROM SomeTable ";
  2. string where = "WHERE "
  3.  
  4. if(a)
  5. {
  6. where = where + " Something = true AND "
  7. }
  8.  
  9. if(b)
  10. {
  11. where = where + " SomethingElse = true AND "
  12. }
  13.  
  14. ... Other check boxes
  15.  
  16. //check to see if any of the check boxes were true
  17. if(where.Length > 6)
  18. {
  19. //remove the last AND from the string
  20. where.Remove(where.Length - 5);
  21.  
  22. sql = sql + where;
  23. }
  24.  
  25.  
Hope this helps

1 1378
hype261
207 100+
In these situations I just dynamically create the SQL command to execute. So for 4 check boxes you would do something like this.

Expand|Select|Wrap|Line Numbers
  1. string sql = "Select * FROM SomeTable ";
  2. string where = "WHERE "
  3.  
  4. if(a)
  5. {
  6. where = where + " Something = true AND "
  7. }
  8.  
  9. if(b)
  10. {
  11. where = where + " SomethingElse = true AND "
  12. }
  13.  
  14. ... Other check boxes
  15.  
  16. //check to see if any of the check boxes were true
  17. if(where.Length > 6)
  18. {
  19. //remove the last AND from the string
  20. where.Remove(where.Length - 5);
  21.  
  22. sql = sql + where;
  23. }
  24.  
  25.  
Hope this helps
Apr 20 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: ash | last post by:
I want search phrase in html file and mark (like Google when I click on "cached") it (phrase). Does somebody know any class, that can help me? Maybe somebody know how could I make this? thanks
0
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match...
46
by: James Harris | last post by:
Before I embark on a new long-term project I'd appreciate your advice on how to split up long names. I would like to keep the standards for command names the same as that for variable names....
2
by: Robert W. | last post by:
In my current work I noticed that I have several circumstances where I need to create little if/else constructs to handle the phrasing of a message. This typically involves a ternary situation like...
6
by: comp.lang.php | last post by:
I am trying to pipe in some auto-generated PHP into php.exe, to no avail: catch {exec echo '<? if (@is_file("./functions.inc.php")) { require_once("./functions.inc.php"); echo...
4
by: =?Utf-8?B?d3VtcGx5?= | last post by:
I read this recently: "The indexing service in XP indexes your files presumably to shorten the time needed to search your hard drive if you are looking for a specific file or part of a phrase...
9
by: C#_Help_needed | last post by:
I need help with the following question. THANKS :) Write a program in c# that takes in a directory as a command line parameter, and returns the longest repeated phrase in ALL text files in that...
1
by: chromis | last post by:
Hi, I've been trying to create a carousel class which takes an array of phrases and then creates a textfield for each one positioning it vertically based on the order it was added. The next stage...
1
by: priyankapendkar | last post by:
Hi, I need to delete all the lines which does not contain the "error" word in a file. so, in vi, I would use :v/error/d and then save the file. How can I achieve this in command line? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.