asp vbscript error handling

Hello Experts,

I am trying to capture a common error in an asp page written in vbscript.  The page allows users to delete a record in a database.  There is a foreign key constraint that keeps users from deleting values that are used in the contraint.  Attempting to delete a record gives this error message:

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC SQL Server Driver][SQL Server]DELETE statement conflicted with COLUMN REFERENCE constraint 'FK_age_resource_age'. The conflict occurred in database 'bmrc', table 'age_resource', column 'age_id'.

/bmrc/admin/catalog/age_detail.asp, line 76

So...

I added the following code to capture the Err.number value

on error resume next
     MM_editCmd.Execute
     'trap errors on delete
     IF err.number <> 0 THEN
          response.Clear()
          Response.Write(Err.number & "<br>")
          Response.Write(err.source)
          response.Write(err.description)
          response.End()
     END IF

This results in the following error page

-2147217900
Microsoft OLE DB Provider for ODBC Drivers[Microsoft][ODBC SQL Server Driver][SQL Server]DELETE statement conflicted with COLUMN REFERENCE constraint 'FK_age_resource_age'. The conflict occurred in database 'bmrc', table 'age_resource', column 'age_id'.

So...

I then edited my code to catch this specific error and tell users to delete the related records

IF err.number <> 0 THEN
     'trap cannot cascade delete
     IF err.number = "-21477217900" THEN
          Response.Clear()
          Response.Write("The age you are trying to delete is still in use. <br> Please remove all related records")
          Response.End()
     ELSE
          response.Clear()
          Response.Write(Err.number & "<br>")
          Response.Write(err.source)
          response.Write(err.description)
          response.End()
     END IF
     END IF
The if err.number = "-21blah" never fires the else always fires.  I also tried if err.number = -21blah without the quotes and the same thing occurs. What am I doing wrong ??

Thanks,

anorglet
LVL 1
anorgletAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ericiCommented:
Here are a couple of things to try/verify:

1) I imagine you're already doing this, but make sure you're using...
Response.Buffer = True
...at the top of your page.  

2) Error Numbers are long integers, not strings, so don't use double quotes.  (I think you mentioned that, but just for clarification, you were right about not using the double quotes.)

3) Do you have any other Response.End statements above the error handling lines?  Response.End will stop processing the page at that point.  I would suggest not using them unless it is necessary, as it give your code multiple exit points and makes any cleanup more difficult.

4) A Select case is usually a bit easier to use with error handling...

Select Case Err.Number
  Case 0 'Do nothing
  Case -21477217900 'Make sure this error number is right
    Response.Clear()
    Response.Write "The age you are trying to delete is still in use. <br> Please remove all related records"

  Case Else  
    response.Clear
    Response.Write Err.number & "<br>"
    Response.Write err.source & "<br>"
    response.Write err.description & "<br>"
End Select
       
Hope that helps!

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anorgletAuthor Commented:
Hark! it works.  I changed the if statements to the select case and it runs fine

Thanks erici!

anorglet
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.