Solved

VB.NET - Insert Query issue with textbox and apostrophe

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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

696 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