Click here to Skip to main content
12,697,783 members (26,256 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.5
I am having trouble writing an sql query with 2 WHERE clauses. This is my current query,
("SELECT * FROM Sales WHERE CD/DVD = '" + typeDisc + "' and SaleDate LIKE '" + Date + "'%");

After a few google searches, I found that after inserting 'and' between the two 'WHERE' clauses, it should return my result.

Any help/examples would be appreciated.
Posted 6-Oct-11 13:04pm
Updated 6-Oct-11 21:47pm
Simon Bang Terkildsen 6-Oct-11 18:27pm
You do not state any problem or question.
Andrew797 6-Oct-11 18:40pm
sorry, figured it out, missed place single qoute. now when i run this query, i get the following message: "No value given for one or more required parameters." Both the fields in my query exists in the database, and when debugging, both variables have data. Any Idea?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I would take a look at the value of the typeDisc (assuming it is a string) and see if there has a single quote in it. If so, you need to escape it by replacing it with two single quotes. On the other hand, if it is an enum variable, then you should use the ToString() function to get its value. Next, look at the Date. If this is a DateTime object, then you will need to use one of the ToString() functions (possibly with the necessary format string) to get the value.

If SalesDate is a datetime datatype, then you may need to do your comparison filter by converting it to a date datatype to ignore the time element:
"...AND CONVERT([date], SalesDate) = CONVERT([date], '" + Date + "')"

Having said this, it is worth noting that sql queries like this are subject to sql injection attacks. Using parameters is a better method.
Bala Selvanayagam 7-Oct-11 4:38am
well said

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 | Mobile
Web02 | 2.8.170118.1 | Last Updated 7 Oct 2011
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