Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have this 3 comboboxes tht I need to filter my query so it only show's the data regarding the 3 combobox combined.

Take for example this:

ID | Motive | Ticket Observation | Price($) | Civilian ID | Due-Date |
1 | Speeding| The car was going at 200mph when the road says 90mph| 350$ | 15441151 | 4th May 2018|

Now imagine that I want to filter the "motive"(speeding) and the "civilian ID"(15441151) and the due date(4th May 2018)?

How can this be possible?
I'm getting this error everytime i use more then 1 filter:
Quote:
The conversion of the string "SELECT * FROM dv_principal where" to type 'Double' is not valid. '


What I have tried:

SELECT * FROM dv_principal where MOTIVE='" + cmbMotive.SelectedValue + "' AND CIV_ID='" + cmbCliente.SelectedValue + " AND DUE_DATE= '"+ dtpDueDate.SelectedText+"'"
Posted
Updated 9-Jun-17 5:49am

You are starting a single quote but not closing it

SELECT * FROM dv_principal where MOTIVE='" + cmbMotive.SelectedValue + "' AND CIV_ID='" + cmbCliente.SelectedValue + " AND DUE_DATE= '"+ dtpDueDate.SelectedText+"'"


Implement some basic debugging and look at the string you are creating

SELECT * FROM dv_principal where MOTIVE='Speeding' AND CIV_ID='15441151 AND DUE_DATE= '4th May 2018'


See the mistake? If a value is numeric you don't need the quotes at all

SELECT * FROM dv_principal where MOTIVE='" + cmbMotive.SelectedValue + "' AND CIV_ID=" + cmbCliente.SelectedValue + " AND DUE_DATE= '"+ dtpDueDate.SelectedText+"'"


You should look to use parameterised queries also as your code is open to SQL Injection attacks, and given the obviously sensitive nature of the data it's something you need to ensure is protected.

PS Note that if your response to this is "still doesn't work" that does not give anyone enough information to help you, your handling of dates looks suspect so that would be the next thing I'd look at.
 
Share this answer
 
Comments
Scribling Doodle 9-Jun-17 11:39am    
I do this everytime because it's easier for me to understand the query... After that, I start creating the parameterized queries using cmd.AddWithValue("@1", cmbMotive.SelectedValue). I'll try to follow your steps and reach back to you with the result. Thanks in advance!
Scribling Doodle 9-Jun-17 11:42am    
Still the same error regarding the double value. Both comboboxes are suposed to give me a single number. Actually I have a table called Motive that has an ID for every motive.
Based on @F-ES Sitecore 's answer I found the problem regarding the double values.

Firstly, when using number's it's recommended to use "&" symbol instead of "+" symbols. Because we are talking about numbers and for the querys itself We need to define what type of date it is. Fixed the problem by replacing all the "+" for "&", making the query like this.

SELECT * FROM dv_principal where MOTIVE='" & cmbMotive.SelectedValue & "' AND CIV_ID=" & cmbCliente.SelectedValue & " AND DUE_DATE= '"+ dtpDueDate.SelectedText +"'"
 
Share this answer
 
Comments
Richard Deeming 9-Jun-17 12:11pm    
None of which would have been a problem if you'd started with a properly parameterized query.
Scribling Doodle 9-Jun-17 12:15pm    
That's the next step as I said in the comments. I start always this way so I can imagine my database while reading the code. Instead of reading tonnes of AddwitValues beneath it... It's simpler to read and more vulnerable yes. I will create the parameterized query now since It's all going great now.

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