Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catch error and reprocess form 1

Status
Not open for further replies.

xsw1971

Programmer
Jun 21, 2001
153
0
0
US
I am adding cftry/cfcatch code to my application and I was wondering how to tell a form processing page to reload itself.

I have a query that gets max(id)+1 from a table and uses that value on an insert statement. Today I had 2 users submit their forms at the exact same moment (first time it's ever happened) and it caused one of the users to get a unique contraint error. What I would like to do is catch the unique contraint error and simply reload the processing page so that the max(id)+1 just gets the next value. I don't want to show anything to the user, just have it do what it was trying to do.

I tried using a meta tag with refresh but that kicked me out back to the form, which is different behavior than if I had clicked on the reload page button.

Thank you in advance for any advice!
 
Try:
Code:
<cflocation url="#CGI.SCRIPT_NAME#" addtoken="no">

That will reload the same page. Put that in the <cfcatch> portion, after you do everything you need to.

[sub]
____________________________________
Just Imagine.
[sub]
 
You should try to get the number when you insert if you're not use an auto number. Rudy can help better with that than i can

but I would try something like the following psudo code.
Code:
<cfquery ...>
get ID
</cfquery>
<cfset newid = queryname.id+1>
<cfset try = true>
<cfloop condition= "try eq true">
  <cftry>
    <cfquery ...>
      insert into...
      where id = #newid#
    </cfquery>
    <cfset try = false>
    <cfcatch type = "database">
      <cfif cfcatch.NativeErrorCode eq "[i]uniqueContraintErrorCodeGoesHere[/i]">
        <cfset newid = newid+1>
        <cfset try = true>
      <cfelse>
        <cfset try = false>
          There was an unexpected database error please contact your administrator.
      </cfif>
    </cfcatch>
  </cftry>
</cfloop>
this isn't tested and I'v never tried it before but in theory, it should add 1 to your new id and try to insert it again using the new id. I put it in a loop incase for some strange reason you had more than 2 submitting at the same time.

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
Thanks for the suggestions!

GUJUm0deL, your method doesn't work for me because I have a whole bunch of form fields and if the user is sent to the processing file without having certain hidden fields defined they are kicked back to the form.

TruthInSatire, your solution worked perfectly and was exactly what I was looking for! I already was getting the next number on insert, but in the case these two users submitted at exactly the same time and happened to grab the next value at the same time.

Thanks a bunch!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top