|
-
Mar 5th, 2015, 11:59 PM
#1
Thread Starter
Addicted Member
Trouble with cmdText and double quotes
Hi, I have an mdb database and I want to execute some queries but some cells are containing double quotes and I'm getting error.
My string:
Code:
Dim dbStr As String = "SELECT * FROM products_tbl WHERE product_name = '" + s + "'"
The error:
Code:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error (missing operator) in query expression 'product_name = 'ASUS X550JK İ5-4200H 4GB 500GB 15.6' VGA 2GB D''.
How can I fix this?
-
Mar 6th, 2015, 12:24 AM
#2
Re: Trouble with cmdText and double quotes
Hi,
Assuming that your variable “s” contains double or single quotes as part of the value you are searching for then you should really use Parameters with your OleDbCommand object to query your database correctly.
Have a read through this:-
OleDbCommand.Parameters Property
Hope that helps.
Cheers,
Ian
-
Mar 6th, 2015, 03:55 AM
#3
Thread Starter
Addicted Member
Re: Trouble with cmdText and double quotes
Thanks for the reply. I searched for a long time however I couldn't adapt oledbcommand.parameters to my code. I'm newbie to vb.net and actually I need some help with searching a column for a specific value.
-
Mar 6th, 2015, 01:16 PM
#4
Re: Trouble with cmdText and double quotes
Code:
Dim dbStr As String = "SELECT * FROM products_tbl WHERE product_name = ?"
'set that as the commandtext of your command object.
'then use
.Parameters.AddWithValue("ProdName", s)
'To add the value to the parameters collection
'Then execute your command and get your results back.
-tg
-
Mar 6th, 2015, 04:47 PM
#5
Thread Starter
Addicted Member
Re: Trouble with cmdText and double quotes
I have to sleep now. I'll check and inform you. Thanks a lot.
-
Mar 7th, 2015, 07:23 AM
#6
Thread Starter
Addicted Member
Resolved: Trouble with cmdText and double quotes
It works like a charm. Thanks again.
Last edited by nikel; Mar 7th, 2015 at 07:24 AM.
Reason: I edited the title
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|