views:

353

answers:

3

Hi there, I have a Classic ASP page that contains the following code to attempt a parametised query;

<%
Set cmdEmail = Server.CreateObject("ADODB.Command")
Set rsEmail = Server.CreateObject("ADODB.Recordset")

cmdEmail.CommandText = "SELECT * FROM VWTenantPropertiesResults WHERE ContentID = ?"
cmdEmail.CommandType = 1
cmdEmail.ActiveConnection = MM_dbconn_STRING
cmdEmail.Parameters.Append
cmdEmail.CreateParameter("@ContentID", 3, 1, , request.Form("ContentID"))

rsEmail.Open cmdEmail
%>

However, the page is now reporting the following error;

Cannot use parentheses when calling a Sub /welcome/default2.asp, line 436 cmdEmail.CreateParameter("@ContentID", 3, 1, , request.Form(ContentID)) -----------------------------------------------------------------------^

Is this some bug with ASP or do I need to change how I attempt parametised queries?

Thank you.

A: 
set cmdEmail = Server.CreateObject("ADODB.Command")

Try and put some debug statements to check, if it is creating an instance of ADODB.Command.

e.g.

If (cmdEmail is Nothing) Then
   Response.Write("could not create the instance")
Else
   Response.Write("created the instance")
End If

Also, remove the With block and see if that makes any difference

dim paramContentID

cmdEmail.CommandText = "SELECT * FROM VWTenantPropertiesResults WHERE ContentID = ?"
cmdEmail.CommandType = 1
cmdEmail.ActiveConnection = MM_dbconn_STRING

set paramContentID = cmdEmail.CreateParameter("@ContentID", 3, 1, , request.Form("ContentID"))
cmdEmail.Parameters.Append paramContentID
shahkalpesh
@shahkalpesh Thanks for your comment. I used the code you suggested and it created the instance but also returned this error;Microsoft VBScript runtime error '800a01f9'Invalid or unqualified reference/welcome/default2.asp, line 441Which indicates an error with the last line of code above. :S
Neil Bradley
@Neil, @shahkalpesh : it's just at typo; an extra space after Append
mjv
@mjv Thank you. That seems to have run through the code, but resulted in "Cannot use parentheses when calling a Sub"
Neil Bradley
@Neil: I am sorry. I removed the With block, but did not qualify the CreateParameter with cmdEmail. The code is now corrected. Which line in specific are you getting the error? and what is the error message?
shahkalpesh
@shahkalpesh Hey, I updated the code with your revision and it now says Incorrect syntax near the keyword 'DEFAULT' on line 437. :S Line 437 is the rsEmail.Open cmdEmail
Neil Bradley
shahkalpesh
@Neil: To give you an example, your querystring might look like ?contentID=NeilBradley. What would you want user to see, if it is such a scenario where contentID is anything but valid?
shahkalpesh
@Neil: Ignore the querystring part, but the questions that I asked still remains the same.
shahkalpesh
@shahkalpesh I have updated the question slightly. I moved cmdEmail.CreateParameter to a new line and it now reports an error about using parenthesis when calling a sub. The ContentID is a hidden field in a form.
Neil Bradley
Basically I need to be able to send details of a particular record in an email, and to ensure that the correct information is sent I have set a hiddenf ield with the contentid and then use the query to grab the details relating to that ContentID.
Neil Bradley
@Neil: See the modified code, that includes a new variable and creation of parameter using the variable.
shahkalpesh
@Neil: What is the content of the hidden field ContentID? If the datatype of it is not in sync with the column you are comparing with, you will get SQL error.
shahkalpesh
@shahkalpesh Thanks man, appreciate your help. The error is now back to the Incorrect syntax near the keyword 'DEFAULT'.
Neil Bradley
The hidden field is as follows <input type="hidden" name="ContentID" value="<%=(rspropertyresults.Fields.Item("ContentID").Value)%>" />
Neil Bradley
ContentID is of Data Type INT
Neil Bradley
@Neil: On this page, put Response.Write(Request.Form("ContentID")) to see what value, it shows on the screen. That will help you understand whether the SQL statement is comparing column with a numeric value.
shahkalpesh
I'm not sure it is getting that far. On this page I have a list of properties from a recordset. Each property has a form to request information about that property. So there are actually multiple forms on the page with the associated ContentID as a hidden field for that property, if that makes sense.
Neil Bradley
@Neil: As I said, put Response.Write(Request.Form("ContentID")) followed by Response.End on the page where you are opening the recordset, to see what it emits.
shahkalpesh
Hmm, shows a blank page now :s
Neil Bradley
@Neil: Try the following Response.Write("hello world") followed by Response.Write(Request.Form("ContentID")) followed by Response.End If it doesn't show anything after hello world, it means the value of ContentID is not being set. If it doesn't show anything in the browser, do a "view source" to see what text is emitted.
shahkalpesh
+1  A: 

Aren't you missing the "Set" statement in there?

ie:

<%
Set cmdEmail = Server.CreateObject("ADODB.Command")
Set rsEmail = Server.CreateObject("ADODB.Recordset")

UPDATE:

In response to Neil's comment of:

Thanks CraigTP. It seems to be creating the instance of ADODB.Command and ADODB.Recordset, but having issues witht he last 4 lines of the code.

I think the last lines of code, should read something like:

cmdEmail.CommandText = "SELECT * FROM VWTenantPropertiesResults WHERE ContentID = ?"
cmdEmail.CommandType = 1
cmdEmail.ActiveConnection = MM_dbconn_STRING
Set adoParam = cmdEmail.CreateParameter("@ContentID", 3, 1, , request.Form("ContentID"))
adoParam.Type = [the datatype of the parameter]
cmdEmail.Parameters.Append(adoParam)

Note that the .CreateParameter method will return a Parameter object. You should assign this returned object to a variable which you then use as a parameter to the .Append method on the Command object's Parameters collection.

See these links for more information:

CreateParameter Method (ADO)
Parameters Collection (ADO)
Append Method (ADO)

Note the section headed "Remarks Parameters Collection" where it states:

You must set the Type property of a Parameter object before appending it to the Parameters collection.

The .Type property of the Parameter object takes a value from the DataTypeEnum enumeration to specify the data type of the parameter. Replace the [the datatype of the parameter] bit of my code above with the relevant data type enum value.

UPDATE 2:

Sorry, didn't notice the question title text had changed!

Ah.. The old classic "Cannot use parentheses when calling a Sub" error, eh?

Well, this is explained here:

Cannot use parentheses when calling a Sub

In a nutshell:

You invoked a subroutine without the Call statement, but used parentheses (). When calling a subroutine without the Call statement, do not use parentheses.

To correct this error:

  • Remove the parentheses from the subroutine invocation.
  • Use the Call statement to invoke the subroutine instead.

There's also a blog post from Eric Lippert that addresses this common error:

What do you mean "cannot use parentheses?"

CraigTP
Thanks CraigTP. It seems to be creating the instance of ADODB.Command and ADODB.Recordset, but having issues witht he last 4 lines of the code.
Neil Bradley
@Neil - I've updated my answer in response to your comment.
CraigTP
+2  A: 

Did you tried just to remove those parenthesis?

cmdEmail.CreateParameter "@ContentID", 3, 1, , Request.Form("ContentID")

As far as I remember, that always happens when you call a function and doesn't use its return value.

UPDATE: Seems the real problem is the line break:


cmdEmail.Parameters.Append _     '' note this "_" character
    cmdEmail.CreateParameter("@ContentID", 3, 1, , Request.Form("ContentID"))
Rubens Farias
Hi. I did try this but it said Expected end of statement, just before the "@ContentID"
Neil Bradley
+1. Yep its that simple, a missing _
AnthonyWJones
Thanks guys. Often just a missing word or character that catches me out.
Neil Bradley