Avatar of dbabbitt
dbabbitt

asked on 

"Type Mismatch" Error Retrieving SQL Record Set

Hi Guys!

How do I get rid of this "Type Mismatch" error? The first SQL call is indeed inserting a row, but for some reason it returns a null of empty ID so I have to go fetch the ID with the second SQL call. This second one is giving me the trouble:
                        Dim BioWebServerConnector
                        Dim BioWebMatch
                        Dim MatchResponse
                        Dim ID
                        
                        Set BioWebServerConnector = CreateObject("BioWeb2Server.Connector")
                        Set BioWebMatch = BioWebServerConnector.Match
                        
                        Set rs = Server.CreateObject("ADODB.Recordset")
                        rs.Open "Biometric_Transactions", cn, adOpenKeyset, adLockPessimistic, adCmdTable
                        rs.AddNew
                        rs("UserID") = Request.Form("User_Logon")
                        rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
                        rs("SupportedDevice") = Request.Form("SupportedDevice")
                        rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
                        rs("Approved") = 0
                        rs.Update
                        ID = rs("ID")
                        rs.Close
                        Set rs = Nothing
                        
                        If (ID = NULL) OR (Len(ID) = 0) Then
                              Dim ConnectionString, MySql
                              Dim Conn
                              ConnectionString = "DSN=BioWeb;UID=sa;PWD=Not4U2C;"
                              Set Conn = Server.CreateObject("ADODB.Connection")
                              Conn.Open ConnectionString
                              MySql = "SELECT MAX(ID) AS ID FROM Biometric_Transactions"
                              Conn.Execute MySql
                              'I'm getting a "Type Mismatch" error here
                              ID = rs("ID")
                              Conn.Close
                              Set Conn = Nothing
                        End If

How do I get rid of this "Type Mismatch" error? Both the server and the client are running on the same laptop, with the IUSR account set up with access to the database.

Thanx

Dave
ASP

Avatar of undefined
Last Comment
alorentz
Avatar of dbabbitt
dbabbitt

ASKER

Possibly my problem is that I am getting a Null back or something because of some security access setting being wrong somewhere - but why does it go ahead and add a row to the Biometric_Transactions table?
Avatar of sybe
sybe

I think the error is here (and maybe elsewhere too). You put a String variable in a Numeric field

 rs("UserID") = Request.Form("User_Logon")
ASKER CERTIFIED SOLUTION
Avatar of william_jwd
william_jwd
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of YZlat
YZlat
Flag of United States of America image

check if rs.eof and rs.bof before retrieving the value

If (ID = NULL) OR (Len(ID) = 0) Then
                         Dim ConnectionString, MySql
                         Dim Conn
                         ConnectionString = "DSN=BioWeb;UID=sa;PWD=Not4U2C;"
                         Set Conn = Server.CreateObject("ADODB.Connection")
                         Conn.Open ConnectionString
                         MySql = "SELECT MAX(ID) AS ID FROM Biometric_Transactions"
                         Conn.Execute MySql
                         'I'm getting a "Type Mismatch" error here
            if not (rs.eof and rs.bof) then
                                     ID = rs("ID")
            end if
                         Conn.Close
                         Set Conn = Nothing
                    End If
Avatar of alorentz
alorentz
Flag of United States of America image

Shouldn't ID be an autonumber fields?
Avatar of YZlat
YZlat
Flag of United States of America image

also you forgot set rs=Conn.Execute MySql


If (ID = NULL) OR (Len(ID) = 0) Then
                         Dim ConnectionString, MySql
                         Dim Conn
                         ConnectionString = "DSN=BioWeb;UID=sa;PWD=Not4U2C;"
                         Set Conn = Server.CreateObject("ADODB.Connection")
                         Conn.Open ConnectionString
                         MySql = "SELECT MAX(ID) AS ID FROM Biometric_Transactions"
                         set rs=Conn.Execute (MySql)
                     
                         if not (rs.eof and rs.bof) then
                                   ID = rs("ID")
                         end if
                         Conn.Close
                         Set Conn = Nothing
                    End If

Avatar of alorentz
alorentz
Flag of United States of America image

Also, this will never give you an ID number:

Set rs = Server.CreateObject("ADODB.Recordset")
                    rs.Open "Biometric_Transactions", cn, adOpenKeyset, adLockPessimistic, adCmdTable
                    rs.AddNew
                    rs("UserID") = Request.Form("User_Logon")
                    rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
                    rs("SupportedDevice") = Request.Form("SupportedDevice")
                    rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
                    rs("Approved") = 0
                    rs.Update
                    ID = rs("ID")  '<---------------------you need to requery for this to work!!! Because I assume ID is autonumber
                    rs.Close

Try this instead:


Set rs = Server.CreateObject("ADODB.Recordset")
                    rs.Open "SELECT MAX(ID) AS ID FROM Biometric_Transactions", cn, 2, 3  '<--need this SQL for the Requery to work!
                    rs.AddNew
                    rs("UserID") = Request.Form("User_Logon")
                    rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
                    rs("SupportedDevice") = Request.Form("SupportedDevice")
                    rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
                    rs("Approved") = 0
                    rs.Update
                    rs.requery  '<----------Requery run SQL again and get the new ID
                    ID = rs("ID")  '<---------------------you need to requery for this to work!!! Because I assume ID is autonumber
                    rs.Close

That should give you the ID that was just Added.  No more code necessary.
Avatar of alorentz
alorentz
Flag of United States of America image

Whoops, that won't work....because you don't select all the fields you need to add.

This should work though:

                    Set rs = Server.CreateObject("ADODB.Recordset")
                    rs.Open "SELECT * FROM Biometric_Transactions ORDER BY ID DESC", cn, 2, 3  '<--need this SQL for the Requery to work!
                    rs.AddNew
                    rs("UserID") = Request.Form("User_Logon")
                    rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
                    rs("SupportedDevice") = Request.Form("SupportedDevice")
                    rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
                    rs("Approved") = 0
                    rs.Update
                    rs.requery  '<----------Requery run SQL again and get the new ID
                    rs.movefirst
                    ID = rs("ID")  '<---------------------you need to requery for this to work!!! Because I assume ID is autonumber
                    Response.write "ID: " & ID
                    rs.Close
Avatar of alorentz
alorentz
Flag of United States of America image

Actually I know that works, I just tested and grabs each new ID, with only that code....good luck!
Avatar of deighc
deighc

@alorentz is correct in that you have to requery the database to get the new auto-id value but he has missed one small (but important) step: when you call the requery method the recordset loses it's cursor position. So you have to save the current cursor position to a variable, requery, then set the cursor position back to what it was before the requery:

Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT MAX(ID) AS ID FROM Biometric_Transactions", cn, 2, 3  '<--need this SQL for the Requery to work!
rs.AddNew
rs("UserID") = Request.Form("User_Logon")
rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
rs("SupportedDevice") = Request.Form("SupportedDevice")
rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
rs("Approved") = 0
rs.Update
cursorPos = rs.absolutePosition ' <--- save the cursor position before the requery
rs.requery  '<----------Requery run SQL again and get the new ID
rs.absolutePosition = cursorPos '<---- set the cursor position back to where it was before the requery
ID = rs("ID")  '<---------------------you need to requery for this to work!!! Because I assume ID is autonumber
rs.Close
Avatar of alorentz
alorentz
Flag of United States of America image

@deighc - you are incorrect...I tested my most recent post and it works fine...noi cursor location necessary because the original query is ordering by DESC.

The query you just posted will not work, because you cannot add records to a query that does not select those records.  You Select MAX(ID), but you cannot add fields to that like I have shown, that's why I used a SELECT * query and ORDER BY  ID DESC.  No cursor necessary, and add new works fine.

See what I mean?
Avatar of alorentz
alorentz
Flag of United States of America image

TO CLARIFY BECAUSE THERE'S BEEN A LOT OF POSTS:

This is the code that works for me to solve the question you aksed:

Set rs = Server.CreateObject("ADODB.Recordset")
                    rs.Open "SELECT * FROM Biometric_Transactions ORDER BY ID DESC", cn, 2, 3  '<--need this SQL for the Requery to work!
                    rs.AddNew
                    rs("UserID") = Request.Form("User_Logon")
                    rs("VerifyFeature") = BioWebMatch.Decode(Request.Form("VerifyFeature"))
                    rs("SupportedDevice") = Request.Form("SupportedDevice")
                    rs("RemoteAddress") = Request.ServerVariables("REMOTE_ADDR")
                    rs("Approved") = 0
                    rs.Update
                    rs.requery  '<----------Requery run SQL again and get the new ID
                    rs.movefirst
                    ID = rs("ID")  '<---------------------you need to requery for this to work!!! Because I assume ID is autonumber
                    Response.write "ID: " & ID
                    rs.Close
Avatar of deighc
deighc

> The query you just posted will not work, because you cannot add records to a query that does not select those records

You're right. I cut-n-pasted the wrong section from your post. You need SELECT *.

As for using ORDER BY ID DESC, well I guess that may work but I wouldn't be 100% happy relying on it. You may have weird behaviour if lots of people of hitting the database at the same time. Even requery itself is a bit dodgy in my opinion (there's no substitute for using a stored procedure that can return the new identity field value).

But if you say it works then I, of course, I believe you.
Avatar of dbabbitt
dbabbitt

ASKER

Now I am getting an "ODBC--call failed." error. But it's a couple of lines downstream so I think I will open up a new Q.
Avatar of alorentz
alorentz
Flag of United States of America image

Heh, heh all that for nothing@
Avatar of dbabbitt
dbabbitt

ASKER

rs.Open "SELECT * FROM Biometric_Transactions ORDER BY ID DESC", cn, 2, 3 gets a read-only error.
Avatar of alorentz
alorentz
Flag of United States of America image

Shouldn't...works fine for me all the time!  

Then use this:

rs.Open "SELECT * FROM Biometric_Transactions ORDER BY ID DESC", cn, adOpenKeyset, adLockPessimistic, adCmdTable
ASP
ASP

Active Server Pages (ASP) is Microsoft’s first server-side engine for dynamic web pages. ASP’s support of the Component Object Model (COM) enables it to access and use compiled libraries such as DLLs. It has been superseded by ASP.NET, but will be supported by Internet Information Services (IIS) through at least 2022.

82K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo