Error because of Primary Key Value Duplicate

dino_p
dino_p used Ask the Experts™
on
I am using a stored procedure (it does not return a Recordset, all it does is add the 2 variables) to add 2 variables (a name, a zip).  A name cannot contain the same zip.  If you try to add it, the ODBC driver gives an error back saying no duplicate values to the primary key.  I want to capture this error (I tried using the Errors collection, no luck) and then notify the user of the mistake.  PLease help.

i'm using sql and doing asp pages.

i've tried your solution, i even tried it yesterday by my own discovery, but keep getting syntax error between goto and InsertError
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Project manager
Commented:
In what language ? VB, C++ ???
What database ? Oracle,SQL,...

In vb:
on error goto InsertError
   ... (execute your stored procedure)
   exit sub
InsertError:
   msgbox err.number & " " & err.description
end sub


Remark: err.number & err.description will give you a number and description. Replace it by a appropiate errormessage

Author

Commented:
i'm using sql and doing asp pages.

i've tried your solution, i even tried it yesterday by my own discovery, but keep getting syntax error between goto and InsertError
Why not trap the error in your Stored Proc and raise a custom error to ASP.
--- SQL ---
INSERT INTO junk_table (Name, Zip) VALUES (@Name,@Zip)
IF @@ERROR <> 0
    BEGIN
        RAISEERROR('This is the error I want to trap', 16, 1)
    END
-----------
You can specify a custom error number also.  So trap for that in your ASP code.

Hope this helps.
No comment has been added lately.
To cleanup this TA, I will recommend to moderators that this question is:
 
*** Answered by: Dhaest ***
 
Please leave any comments here within the next seven days, or finalize the question if you own it
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
peterchen
EE Cleanup Volunteer

per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial