Results 1 to 6 of 6

Thread: Trouble with cmdText and double quotes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    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?

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    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.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Trouble with cmdText and double quotes

    I have to sleep now. I'll check and inform you. Thanks a lot.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Resolved 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
  •  



Click Here to Expand Forum to Full Width