Solved

ASP /. SQL does not like an apostrophe

Posted on 2004-09-10
9
361 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

17 Experts available now in Live!

Get 1:1 Help Now