Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 29th, 2008, 11:24 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default parametising sql statements in ASP classic

I was wondering if someone could spot where I have gone wrong in my code please, as I have gone blind looking for the error in this:

Code:
strName = "myname"

Set objConnection = Server.CreateObject("ADODB.Connection")
objConnection.Open "Driver={SQL Server}; SERVER=localhost;UID=no_test;PWD=test;DATABASE=No_Test"

strSql = "SELECT * FROM test WHERE test = ?;"
set objCommand = Server.CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = strSql

objCommand.Parameters.Append objCommand.CreateParameter ("name", adVarChar, adParamInput, 50, strName)
Set rs = server.CreateObject("ADODB.Recordset")
rs.CursorLocation = adUseClient
rs.LockType = adLockReadOnly

rs.Open objCommand.Execute
Error I get is:

Code:
ADODB.Command error '800a0bb9'

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

/test2.asp, line 21
Line 21 is this line:
Code:
objCommand.Parameters.Append objCommand.CreateParameter ("name", adVarChar, adParamInput, 50, strName)
 
Old May 29th, 2008, 11:30 AM
Authorized User
 
Join Date: May 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have also tried adding this:
Code:
set objParameter2 = objCommand.CreateParameter( , adVarChar, adParamInput, 30, strName)
objCommand.Parameters.Append objParameter2
objParameter2.value = strName
in place of this:
Code:
objCommand.Parameters.Append objCommand.CreateParameter ("name", adVarChar, adParamInput, 50, strName)
 
Old June 16th, 2008, 09:20 AM
Registered User
 
Join Date: Jun 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your SQL Select is shown as

strSql = "SELECT * FROM test WHERE test = ?;"

I'm not sure if this is the source of your problem but should that not be...

strSql = "SELECT * FROM test WHERE test = '?';"

HTH

I have not failed... I've just found 10,000 way that don't work!
 
Old June 16th, 2008, 02:46 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I think it is likely that the root of your problem is that you didn't #include the "adovbs.inc" file.

See here:
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=160

You also have a problem on this line:

    rs.Open objCommand.Execute

Won't ever work. You are trying to invoke two methods of doing the same thing on the same line. I'm pretty sure all you want here is

    rs.Open objCommand

*******

Mych: No. Never. If you use '?' then you just turned the question mark into a simple literal string. That is, a real question mark. It *MUST* be by itself to be a parameter place holder.
 
Old June 18th, 2008, 04:05 AM
Registered User
 
Join Date: Jun 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Pedant,

Sorry, have never across come the use of ? on its own...

What would the sql statement select?

The way I understand, it will select all records from the table test where the field test would equal the variable ?...

Confusion!:( Can you assign ? as variable? This why I though it need to be enclosed in single quotes.

What is objParameter?

Sorry for my ignorance.
Mych


I have not failed... I've just found 10,000 way that don't work!
 
Old June 18th, 2008, 02:38 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

We use ? in parameterized queries with Access the same way you would use @xxx in parameterized stored procedures with SQL Server.

Each ? represents one parameter that will have it's value filled from one ADODB.Parameter.

So if you did something like
    SELECT * FROM table WHERE id = ? AND postdate > ? AND topic = ?
then you would need three ADODB.Parameter objects appended to the Parameters collection of the ADODB.Command object.

The data type of each parameter is determined by the type you assign to the created ADODB.Parameter object. If you mess up (e.g., if you use a text parameter for the id when id is a numeric field in the DB), you can and will get an error. You don't need to worry about '...' or #...# delimiters (for text and date, respectively) and you are *NOT* subject to SQL Injection attacks.

Do you need a full blown example??





Similar Threads
Thread Thread Starter Forum Replies Last Post
Classic ASP calling a SQL 2000 SP jonsey Classic ASP Databases 6 July 30th, 2007 04:03 PM
Can i use asp within sql statements knight Classic ASP Databases 43 May 24th, 2007 09:32 AM
accessing MS SQL DB from Classic ASP script crmpicco SQL Server ASP 2 June 1st, 2006 03:43 PM
CASE Statements in T-SQL atcs2152 SQL Server 2000 3 April 28th, 2006 10:53 AM
SQL Statements marmer Classic ASP Basics 3 November 13th, 2003 01:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.