Solved

ASP /. SQL does not like an apostrophe

Posted on 2004-09-10
9
378 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
[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
  • 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
Technology Partners: 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!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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