views:

252

answers:

2

Hi,

I have a classic ASP page with some code to check if an email exists in the table as follows;

<%
    '' //Check the submitted email against existing ones in the database
    set CmdCheckEmail = server.CreateObject("ADODB.Command")
    CmdCheckEmail.ActiveConnection = MM_dbconn_STRING
    CmdCheckEmail.CommandText = "SELECT COUNT(ReferredEmail) AS 'CountEmail' FROM TenantReferral WHERE ReferredEmail = '" & Request("Email") & "'"
    Response.Write(CmdCheckEmail.CommandText)
    CmdCheckEmail.CommandType = 1
    CmdCheckEmail.CommandTimeout = 0
    CmdCheckEmail.Prepared = true
    CmdCheckEmail.Execute()

    countEmail = CmdCheckEmail("CountEmail")

    set CmdCheckEmail = nothing
    conn.close
    set conn = nothing

    If(countEmail >= 1) Then
     Message = Message & "<p>This email address has already been referred.</p>"
    End If
%>

However, the page is reporting the following error;

SELECT COUNT(ReferredEmail) AS 'CountEmail' FROM TenantReferral WHERE ReferredEmail = '[email protected]'

ADODB.Command error '800a0cc1'

Item cannot be found in the collection corresponding to the requested name or ordinal.

/default2.asp, line 19

Line 19 is as follows;

countEmail = CmdCheckEmail("CountEmail")

The email does exist in the table and the table simply has the following columns; ReferredEmail and ReferredCode

I wondered if anyone might be able to shed any light on this error?

Thank you.

A: 

Note sure what database you are using but try changing your sql to:

SELECT COUNT(ReferredEmail) AS CountEmail FROM TenantReferral WHERE ReferredEmail = '[email protected]'

Then change

CmdCheckEmail.Execute()    
countEmail = CmdCheckEmail("CountEmail")

to

set rs = CmdCheckEmail.Execute()
countEmail = rs("CountEmail")

Also, you have a SQL injection issue with that query. You should be using parameterized queries.

RedFilter
@Orbman - I'm using an MSSQL database, but still reporting the same error whether using 'CountEmail' or CountEmail :(
Neil Bradley
You are using the cmd object incorrectly, see my edit.
RedFilter
Thanks Orbman. I'm now getting an Object required: 'conn'/default2.asp, line 20 error.
Neil Bradley
You need to read up on ADO rather than copy and paste. You must create and open a connection object, passing it the connection string for your database.
RedFilter
See http://www.w3schools.com/ADO/ado_ref_connection.asp and http://www.connectionstrings.com/.
RedFilter
Hi. I have my database connection string stored in a connection include referenced by the MM_dbconn_STRING.
Neil Bradley
A: 

CmdCheckEmail("CountEmail") tries to access the default member of the Command object, which is the parameters collection. However, you don't want to access a parameter but a field of the resulting recordset.

Try this (not tested):

Set rs=CmdCheckEmail.Execute()

countEmail = rs("CountEmail")

Apart from that, beware: This line:

CmdCheckEmail.CommandText = "SELECT COUNT(ReferredEmail) AS 'CountEmail' FROM TenantReferral WHERE ReferredEmail = '" & Request("Email") & "'"

is vulnerable to an SQL injection attack.

Never embed literal strings into SQL statement; use parameters instead. (In this case, you would do that using the Command.Parameters collection.)

Martin B