?
Solved

VB.NET - Insert Query issue with textbox and apostrophe

Posted on 2010-08-31
4
Medium Priority
?
579 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 1200 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 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

752 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