The company I work for is holding a fundraising event and asked me to put together a little web application to accept donations through PayPal. That sounded simple enough, but the deadline was tight, so I decided to dredge up something similar I had created in Classic ASP/VBScript over 10 years ago and reuse it (along with an Access database). But then the requirement changed and became more complex (same deadline though), and, to make a long story short, I ended up with a mish-mash of code and an application that seemed to work fine...until today, when a user reported seeing the wrong data on the "Thank You" page of the application. It is clear to me that at some point after a record is inserted into the database, the code occasionally returns the wrong id, but I am unable to duplicate the error or find the problem in the code, and I was hoping someone might be able to help me.
In the most simple terms, here's how the application works:
- The user has a choice of signing up for three different events and must make a PayPal donation to participate in the event he chooses. There are all kinds of additional choices he can make as well, depending on the event he signs up for. So basically, the user fills out a standard html form, and there's a bunch of jQuery stuff going on in the background to help guide the user through the various choices.
- Once the user has made all of the required choices and hits the "submit" button, I use jQuery to send an ajax call to a Classic ASP/VBScript page which inserts the user's data into an Access database and returns the id of the newly inserted record as a json response.
- The ajax success function takes the new id, inserts it into a field called "item_number" on a hidden PayPal form and then submits the PayPal form to PayPal. The user is then taken to PayPal to make the payment.
- I have PayPal's Instant Payment Notification feature set up so that when the user makes the payment, a notification is sent to a listener on our server. The notification contains the item_number in the query string, and the listener takes the item_number, goes back to the Access database and updates that record to show that the user has actually paid.
- Finally, PayPal returns the user to a thank you page on our server. The thank you page picks up the item_number query string, queries the Access database for the record matching that id, and displays confirmation of whatever the user has signed up for (this is the "thank you" page).
The problem is that once in a while in Step 2 after the data is inserted into the database (the data is inserted correctly), the wrong id gets returned, which in turn adversely affects the rest of the steps. The other problem is that sometimes when PayPal returns the user to the "thank you" page, it appends the item_number query string twice, which seems to throw a 500 error on the server...so the user gets a 500 page instead of a thank you. At first I thought maybe two people were signing up at once and Access was just being lame, but I was able to confirm that only one user had signed up at the time.
I think the problem must be in the code for Step 2, but I'm completely missing it. Here's the code (I've left out most of the fields here to save space):
'Declare field names
Dim Email
Dim EventType
'Declare variable for the id of the inserted record
Dim currentId
Email = Request.Form("Email")
EventType = Request.Form("EventType")
'if EventType = tournament, then check if user already registered
If EventType = "tournament" Then
Dim rscheckregistration
Dim rscheckregistration_cmd
Dim checkRegistration
Dim hasAlreadyRegistered
hasAlreadyRegistered = 0
Set rscheckregistration_cmd = Server.CreateObject ("ADODB.Command")
rscheckregistration_cmd.ActiveConnection = connregistration
rscheckregistration_cmd.CommandText = "SELECT COUNT(*) AS Total FROM tournament WHERE EventType = 'tournament' AND Email = '" & Email & "'"
rscheckregistration_cmd.Prepared = true
Set rscheckregistration = rscheckregistration_cmd.Execute
checkRegistration = rscheckregistration("Total")
rscheckregistration.Close
Set rscheckregistration = Nothing
if checkRegistration > 0 Then
hasAlreadyRegistered = 1
end if
'do not allow user to register more than once
if hasAlreadyRegistered = 1 Then
Response.ContentType = "application/json"
Response.Write("{ ""type"":""Already Registered""}")
end if
if hasAlreadyRegistered = 0 Then
Set rsregister = Server.CreateObject("ADODB.Recordset")
rsregister.ActiveConnection = connregistration
rsregister.Source = "SELECT * FROM tournament"
rsregister.CursorType = 2
rsregister.LockType = 3
rsregister.CursorLocation = 3
rsregister.Open()
rsregister.AddNew
rsregister.Fields("Email") = Email
rsregister.Fields("EventType") = "tournament"
'get the id of the record that was just inserted
rsregister.Update
bookmark = rsregister.absolutePosition
rsregister.Requery
rsregister.absolutePosition = bookmark
currentId = rsregister("TournamentId")
rsregister.Close
Set rsregister = Nothing
Response.ContentType = "application/json"
Response.Write("{ ""type"":" & currentId & "}")
end if
'handle EventType = raffle and and EventType = casino
Else
Set rsregister = Server.CreateObject("ADODB.Recordset")
rsregister.ActiveConnection = connregistration
rsregister.Source = "SELECT * FROM tournament"
rsregister.CursorType = 2
rsregister.LockType = 3
rsregister.CursorLocation = 3
rsregister.Open()
'insert the record
rsregister.AddNew
rsregister.Fields("Email") = Email
rsregister.Fields("EventType") = EventType
'get the id of the newly inserted record
rsregister.Update
bookmark = rsregister.absolutePosition
rsregister.Requery
rsregister.absolutePosition = bookmark
currentId = rsregister("TournamentId")
rsregister.Close
Set rsregister = Nothing
Response.ContentType = "application/json"
Response.Write("{ ""type"":" & currentId & "}")
End if