Solved

VB.NET - Insert Query issue with textbox and apostrophe

Posted on 2010-08-31
4
568 Views
Last Modified: 2012-05-10
Hello experts,  

Not sure how to go about this, but I created a windows application that has a richtextbox.  Behind code will insert whatever that is typed in this richtextbox to a SQL Server 2005 database table.  It works fine, but when a comma is use, it gives me an error.

Example:

John's car was stolen.

Above example thinks that the apostrophe is the closing.  So if I look from code behind, my syntax query would look like the following:

Insert into Table1(ID, Description)
Values(1, 'John's car was stolen')

0
Comment
Question by:holemania
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33566856
double up the single-quotes


Insert into Table1(ID, Description)
Values(1, 'John''s car was stolen')
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 300 total points
ID: 33566873
Or if you are building the string, then use replace as shown below.
(But you really should be using parameterized queries..)
Dim query as String = "Insert into Table1(ID, Description) Values(1, '" & strDescription.Replace("'","''") & "')"

Open in new window

0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 200 total points
ID: 33566899
I would suggest to use parameters with your insert-command

http://www.java2s.com/Code/VB/Database-ADO.net/UseParametersinyoursqlcommand.htm
0
 

Author Closing Comment

by:holemania
ID: 33569518
Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question