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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catching Errors? Redirect Page for duplicate DB entry?

Status
Not open for further replies.

maverik59

Programmer
Oct 30, 2002
67
0
0
GB
Hi,
I get an error message "Error Executing Database Query duplicate key error" Is it possible to catch this error or detect the error in advance and then return the text "sorry you have already entered this data previously" rather than getting the error message?

any Help Much Appreciated.
SI
 
The simplest way to do this would be query the database right before you insert your record to see if the record already exists.

<CFQUERY NAME=&quot;check_record&quot; DATASOURCE=&quot;#data#&quot;>
SELECT PrimaryKeyName
FROM dbo.TableName
WHERE PrimaryKeyName = '#Form.PrimaryKeyName#'
</CFQUERY>


Then, check your query results

<cfif check_record.RecordCount EQ '0'>
<CFQUERY NAME=&quot;insert_record&quot; DATASOURCE=&quot;#data#&quot;>
INSERT INTO dbo.TableName (PrimaryKeyName,etc, etc, etc)
VALUES ('#Form.PrimaryKeyName#', #etc#, #etc#, #etc#)
</CFQUERY>

<cfelse>

THIS RECORD ALREADY EXISTS!
</cfif>

It's a little crude and you will probably have to tweak the code a little bit, but this should give you the general idea.

I hope this helps!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top