Solved

VB.NET - Insert Query issue with textbox and apostrophe

Posted on 2010-08-31
4
557 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now