Microsoft KB Archive/246069

= Encoding form data for SQL Server input and output to HTML =

Article ID: 246069

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft Internet Information Services 5.0

-



This article was previously published under Q246069



SUMMARY
When you are working with records in a database using Microsoft Active Server Pages (ASP) for Internet Information Services (IIS), errors may occur when the text has not been properly formatted for creating a SQL Server clause.

For example, when you create an ASP page to store information posted from an HTML form to a database, you may see an error message similar to the following:

Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)

[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression ''.

/ /, line



MORE INFORMATION
The error message above occurs when you try to submit a name with an apostrophe, (for example "O'Malley"), and then insert that name into a SQL Server clause without first "escaping" it. This can result in a SQL Server clause that resembles the following: INSERT INTO Table (txtName VALUES ('O'Malley') Because SQL Server uses the apostrophe as a text delimiter, the apostrophe in the name inadvertently signifies an end to the data, which causes the error message to occur. This text can be "escaped" by using two apostrophes, for example: INSERT INTO Table (txtName VALUES ('O''Malley') Obviously, it is not ideal to mandate that all users on a Web site enter two apostrophes for each desired apostrophe. In addition, when text is later extracted from a database, certain formatting characters are ignored during HTML parsing on a client (for example CR-LF, TAB, and so on.)

An ASP solution
The following ASP code defines two functions that are designed to handle some simple parsing of information for input/output of SQL Server data, and can easily be expanded to incorporate more functionality:   FixForSQL parses a string for input to SQL Server Function FixForSQL(tmpText1) ' define a working variable Dim tmpText2 ' populate our working variable tmpText2 = tmpText1 ' compact a CR-LF sequence as CR to save space tmpText2 = Replace(tmpText1,vbCrLf,Chr(13)) ' replace each apostrophe with two apostrophes tmpText2 = Replace(tmpText2,Chr(39),String(2,39)) ' return the fixed string FixForSQL = tmpText2 End Function   FixForHTML parses a string for output to HTML Function FixForHTML(tmpText1) ' define a working variable Dim tmpText2 ' populate our working variable tmpText2 = tmpText1 ' replace each CR with a line break tag and CR-LF tmpText2 = Replace(tmpText2,Chr(13)," " & vbCrLf) ' replace each TAB character with four non-breaking space tags tmpText2 = Replace(tmpText2,Chr(9),"&#xa0;&#xa0;&#xa0;&#xa0;") ' return the fixed string FixForHTML = tmpText2 End Function 

Practical example - Guestbook application
The following steps demonstrate how to use the above functions to create a simple guestbook application using ASP and a Microsoft Access database:   Copy the following ASP code and save it to a folder in your Web site with at least IIS script access enabled and Copy the following ASP code and save it to a folder in your Web site with at least IIS "script" access enabled and Everyone - Change NTFS permissions: <% @Language="VBScript" %> <% Option Explicit

' define our variables Dim strMethod Dim strURL Dim strTitle Dim strSQL Dim objCN Dim objRS

' get some environment variables With Request strMethod = UCase(.ServerVariables("REQUEST_METHOD")) strTitle = UCase(.ServerVariables("SERVER_NAME")) & " Guestbook" strURL = LCase(.ServerVariables("URL")) End With

' open our database connection Set objCN = Server.CreateObject("ADODB.Connection") objCN.Open "driver={Microsoft Access Driver (*.mdb)};uid=admin;pwd=;" & _ "dbq=" & Server.MapPath(".\guestbook.mdb")

' if the request was a POST then a guest submitted If strMethod = "POST" Then Dim strName, strComments ' get the form data and format it   strName = FixForSQL(Request.Form("txtName")) strComments = FixForSQL(Request.Form("txtComments")) ' build the SQL string strSQL = "INSERT INTO tblGuestbook (txtName, txtComments)" strSQL = strSQL & " VALUES ('" & strName & "','" & strComments & "')" ' execute the SQL objCN.Execute(strSQL) End If

' create a SQL string to read all guest entries strSQL = "SELECT * FROM tblGuestbook" Set objRS = objCN.Execute(strSQL)

%> <%=strTitle%>

<%=strTitle%>

<% ' only show the guestbook when there are entries If Not objRS.EOF Then %>

<% End If %>

" method="POST"> Name  Comments  <input type="submit" value="Submit Comments">

<% Function FixForSQL(tmpText1) Dim tmpText2 tmpText2 = tmpText1 tmpText2 = Replace(tmpText1,vbCrLf,Chr(13)) tmpText2 = Replace(tmpText2,Chr(39),String(2,39)) FixForSQL = tmpText2 End Function

Function FixForHTML(tmpText1) Dim tmpText2 tmpText2 = tmpText1 tmpText2 = Replace(tmpText2,Chr(13)," " & vbCrLf) tmpText2 = Replace(tmpText2,Chr(9),"&#xa0;&#xa0;&#xa0;&#xa0;") FixForHTML = tmpText2 End Function %>                       </li> Open Microsoft Access, create a new database, and then save it as Guestbook.mdb in the same folder as the above ASP page. Create a new table in the database named tblGuestbook with the following fields:

Save the table to the database, and then close Microsoft Access.</li></ol>

Testing the Guestbook Application
 * 1) When you browse the page for the first time, no data is displayed. When information has been submitted, any guestbook entries will appear. Entering apostrophes in names (for example O'Brien, O'Sullivan, and so on) or contractions (such as don't, can't, and so on) are properly formatted and stored in the database.
 * 2) When information has been submitted, any guestbook entries will appear. Entering apostrophes in names (for example O'Brien, O'Sullivan, and so on) or contractions (such as don't, can't, and so on) are properly formatted and stored in the database.
 * 3) Entering apostrophes in names (for example O'Brien, O'Sullivan, and so on) or contractions (such as don't, can't, and so on) are properly formatted and stored in the database.

<div class="references_section">