Click here to Skip to main content
11,411,494 members (67,097 online)
Rate this: bad
good
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
Edited 6-Oct-11 21:47pm
sravani.v15.8K
v2
Comments
Simon Bang Terkildsen at 6-Oct-11 18:27pm
   
You do not state any problem or question.
Andrew797 at 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
good
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.
  Permalink  
Comments
Bala Selvanayagam at 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
0 OriginalGriff 223
1 Sascha Lefévre 155
2 Sergey Alexandrovich Kryukov 149
3 ProgramFOX 90
4 Dave Kreskowiak 90
0 Sergey Alexandrovich Kryukov 8,920
1 OriginalGriff 7,128
2 Maciej Los 3,440
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web03 | 2.8.150414.5 | Last Updated 7 Oct 2011
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