NCS Logo - Click for home page Northstar Developer Center
Platforms
All Platforms
.NET Framework (1.x - 4.x)
Active Server Pages
ASP.NET
C#
SQL Server
VB.NET
Visual Basic

Keywords
.NET Data Types
.NET E-mail
.NET Events
.NET Functions
.NET Object Programming
.NET System.Configuration
.NET System.Diagnostics
.NET System.IO
.NET System.Net
.NET System.Net.Sockets
Active Data Objects
ASP Architecture
ASP Black Belt
ASP Built-in Functions
ASP Built-in Objects
ASP Debugging
ASP Performance
ASP Security
ASP Syntax
ASP.NET Authentication
ASP.NET Controls
ASP.NET Data Access
ASP.NET Features
ASP.NET Master Pages
ASP.NET Page Events
ASP.NET Security
ASP.NET ViewState
Atom
Certifications
COM, DCOM, COM+
Data Access
E-Mail
Errors
Exporting Data
HTML Tips
IIS
Object-Oriented Programming
RSS
SQL
Uncategorized ASP Tips
VB API Programming
VB Forms
VB Syntax
XML

Book Support
Visual Basic 6 Bible
ASP Bible
ASP Weekend Crash Course
ASP.NET At Work
Creating Web Services

Cleaning Your SQL Data

Written by Eric Smith, Northstar Computer Systems LLC

One of the big problems people have when they are writing SQL within ASP is dealing with data supplied by users. You may have some code that looks like this:
Dim strSQL
strSQL = "SELECT * FROM Customers WHERE CompanyName = '" _
   & Request.Form("Keywords") & "'"
This will return all the customers where the CompanyName field is equal to whatever the person put in the Keywords field on a form. The problem comes when you have a name with a single quote in it, such as the name O'Reilly. The single quote character breaks the SQL statement.

The easy thing to do is to "clean" the input data before putting it into the SQL statement. Here's a quick function you can use, and the SQ constant that holds a single quote.

Const SQ = "'"
Function Clean(strData)
   Clean = Replace(strData, SQ, SQ & SQ)
End Function
The previous chunk of code looks like this with the Clean function in place:
Dim strSQL
strSQL = "SELECT * FROM Customers WHERE CompanyName = '" _
   & Clean(Request.Form("Keywords")) & "'"

Keywords: [ Uncategorized ASP Tips ]

Publication Date: 7/1/2000