?
Solved

asp vbscript error handling

Posted on 2003-04-01
2
Medium Priority
?
1,132 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:anorglet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
erici earned 400 total points
ID: 8248093
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
 
LVL 1

Author Comment

by:anorglet
ID: 8248176
Hark! it works.  I changed the if statements to the select case and it runs fine

Thanks erici!

anorglet
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question