Solved

ASP /. SQL does not like an apostrophe

Posted on 2004-09-10
9
373 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
Industry Leaders: 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
If-Then-Else ASP problem 6 78
My Do While is Not Producing Anything 15 88
Sending ASP to server side 8 43
Pass SQL to JSON. Page is in classic ASP and using Jquery 4 58
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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