Solved

ASP /. SQL does not like an apostrophe

Posted on 2004-09-10
9
367 Views
Last Modified: 2008-01-16
Hello,

I have a textbox called "txtComment".   I store the value as:
comment = Request.Form("txtComment")
and then the value of this text bo gets inserted into a SQL Server database.

Well, if the user puts in the textbox the following string:
That is the cat's hat

it will fail because of the apostrophe in "cat's".

What can I do to prevent this?
0
Comment
Question by:rudyflyer
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 19

Expert Comment

by:peh803
ID: 12031115
when building your insert statement, you must replace the single apostrophes with double apostrophes...

so, like this:

comment = Request.Form("txtComment")

sSQL = "INSERT INTO myTable (comments) VALUES ('"&replace(comment, "'", "''")&"'"

regards,
peh803
0
 
LVL 19

Expert Comment

by:peh803
ID: 12031117
the reason, of course, being that a single quote will break a sql string unless escaped -- the escape syntax for single quotes in T-SQL is just to make each occurrence of once single quote into two single quotes.
0
 

Expert Comment

by:rmcewan
ID: 12031264
For years of VB and ASP coding, I've had a little function called TrimSQL. All this does is replacement of single quotes with two single quotes. I use TrimSQL on every sql call, thus:

rs.open TrimSQL("blah blah blah")
conn.execute TrimSQL(sSQL)

<%
function TrimSQL(sSQL)

      TrimSQL = trim(replace(sSQL,"'","''"))

'-- debug code after here
'e.g.:  response.write "<br>SQL call @" & now() & ": " & sSQL & "<br>"

end function
%>

Using a function in this way, and EVERY time you talk to the database means you'll never need to worry about the 'ol Apostrophe problem again. Additionally, TrimSQL can be modified for debugging purposes, for instance, to log every SQL call made by your code. Just put TrimSQL in with your other common functions and <!--#include the file on every asp page.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:rudyflyer
ID: 12031326
rmsewan,

I like that function.  Can you help me a little bit more as to how I can implement it into my code?  I have a stored procedure that does the insert.  Here is part of the code:

Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")


strSQL = "spInsert" & chr(39) & name& chr(39) & ", " & chr(39) & comment & chr(39)
objRS.Open strSQL, objConn, , adCmdStoredProc
0
 
LVL 4

Expert Comment

by:divt
ID: 12031420
Allow me to adjust the code for you, but please give alll the points to rmsewan:


Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")


strSQL = "spInsert" & chr(39) & name& chr(39) & ", " & chr(39) & comment & chr(39)
objRS.Open TrimSQL(strSQL), objConn, , adCmdStoredProc



Of course, you need to add this to your file as well:

function TrimSQL(sSQL)

      TrimSQL = trim(replace(sSQL,"'","''"))

'-- debug code after here
'e.g.:  response.write "<br>SQL call @" & now() & ": " & sSQL & "<br>"

end function
0
 

Expert Comment

by:rmcewan
ID: 12031486
I take it that your stored proc is returning a resultset, hence your use of a recordset to execute the proc? I'm Oracle for the most part and we don't have the ability to easily return result sets from procedures, so I always use conn.execute

strSQL = "spInsert '" & TrimSQL(name) & "','" & TrimSQL(comment) & "'"
(i don't see why you're throwing the apostrophes in with CHR(39) )

Now, it would be possible to write a much smarter function that would parse and fix an entire SQL statement for you - but I've never had the need.
0
 

Accepted Solution

by:
rmcewan earned 125 total points
ID: 12031522
sorry folks... I may have caused some confusion there with my original TrimSQL examples. With that particular form, you need to TrimSQL every string as you put it into the overall SQL string. i.e.

strSQL = "spInsert '" & TrimSQL(name) & "','" & TrimSQL(comment) & "'"
is fine, but
strSQL = TrimSQL("spInsert '" & name & "','" & comment & "'")
would fail because it'll incorrectly replace the ' around the arguments.

As I said, you *could* write a more sophisticated version that would parse a full SQL command as in the second case. In fact, you could try googling up a version.
0
 
LVL 4

Expert Comment

by:divt
ID: 12031617
rmcewan,  Thank you for correcting me!  I feel like a total idiot for doing objRS.Open TrimSQL(strSQL)!!!
0
 

Author Comment

by:rudyflyer
ID: 12031632
And thank you too divt.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

14 Experts available now in Live!

Get 1:1 Help Now