We help IT Professionals succeed at work.

How to handle error in asp

lyu97
lyu97 asked
on
Medium Priority
309 Views
Last Modified: 2007-12-19
Hi experts,
I have a asp page which upload files to database,I want to add some error handler code to avoid user upload same file twice, but the asp seems doesn't like On Error GoTo ErrorHandler statement, and I tried use On Error Resume Next and it doesn't work either. here is the code to upload file to database:
<!-- #Include file="adovbs.inc" -->

<%
Set theForm = Server.CreateObject("ABCUpload4.XForm")
Set theField = theForm("filefield")(1)
if theField.FileExists then

dim constring
dim objcon
set objcon=server.CreateObject("ADODB.Connection")

constring="Provider=MSDAORA.1;Password=XXX;User ID=XXX;Data Source=XXXX;Persist Security Info=True"
objcon.ConnectionString=constring
 objcon.cursorLocation=adUseClient
objcon.open

  dim rs,strattach
strattach="select id,empid,filename,attachcontent from attachtable "

  Set rs = Server.CreateObject("ADODB.Recordset")
 rs.Open strattach, objcon,adOpenDynamic ,adLockOptimistic
  rs.AddNew
  rs("id")=0
  rs("emdid")="1111"
 
  rs("filename")=theField.SafeFileName
 
 rs("attachcontent").Value = theField.Data
rs.Update
 rs.Close
 objcon.Close
End If
Response.Redirect "dbupload.htm"
%>
if you have any idea, please let me know(how to use error handler and where to put these code? is that possiable to pop up a error message box to the user?)

Thanks in advance!

Lin.
   
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2005
Commented:
Place the following text on the top of the ASP page:
<%On Error Resume Next%>

Place this code near the end of your page (you'll need to replace my objConnection with your objcon:


'VBScript errors
If Err.Number > 0 Then
    Response.write("<p><hr>The following VB Errors Occured<p>")
     Response.write("Error Number: " & Err.number & "<br>")
     Response.write("Error Source: " & Err.source & "<br>")
     Response.write("Error Description: " & Err.description & "<br>")
End If


'Connection errors
dim iCounter
if objConnection.Errors.count > 0 then
    Response.write("<p><hr>The following Connection Errors Occured<p>")
    for iCounter = 0 To objConnection.Errors.Count - 1
          Response.write("Error Number: " & objConnection.errors(iCounter).number & "<br>")
          Response.write("Error Description: " & objConnection.errors(iCounter).description & "<br>")
          Response.write("SQL State: " & objConnection.errors(iCounter).SQLState & "<br>")
          Response.write("Native Error: " & objConnection.errors(iCounter).NativeError & "<br>")
    next
end if
CERTIFIED EXPERT
Top Expert 2005

Commented:
I don't know about the pop up box, but you can encorporate the above code into a user-friendly page.

You can also place this code in an include file and either call it as a function near the bottom of each page or you can simply include the file near the bottom of each page.

Fritz the Blank

Author

Commented:
Hi fritz blank,
thanks.when I upload the same file second time, it printed out the follow page:
The following VB Errors Occured

Error Number: 3219
Error Source: ADODB.Recordset
Error Description: Operation is not allowed in this context.

actually the error occurred because the primary key is duplicated,but why they gave me that error description?
when I run the error(3219) from immediate window(in VB) it gave me the follow description about this error:
Run-time error '3219'
Application-defined or object-defined error

another thing,I've tried several time after I add your code to my page, some times(I caught 2) the error page didn't display(of course, the file didn't uploaded,but why did not show that error page?)
if you have any clue,please let me know.
Lin

Author

Commented:
Hi fritz blank,
forget the >>
another thing,I've tried several time after I add your code to my page, some times(I caught 2) the error
page didn't display(of course, the file didn't uploaded,but why did not show that error page?)
.it's my fault.
Lin
CERTIFIED EXPERT
Top Expert 2005

Commented:
Please make sure that you have changed objConnection to objcon to match your code. Also, it is necessary to call the error script before closing the connection or else you may not get any connection error information.


Error trapping in ASP is less than perfect...

In the latest version of IIS, there is a better mechanism using the ASPError object. You can create customized pages and save them on the server to give the user a consistent, friendly looking recovery from errors. It also adds the much-needed line # for trouble shooting:

    Dim  objASPError
    Set objASPError = Server.GetLastError()
    If objASPError.Number <> 0 then
         %>      
         <h3>Your request has generated the following ASP Errors:</h3>
         <h6>(Some of these errors may be the same as those in the Database Errors if present)</h6><HR>
         Error Number: <%= objASPError.Number %> <BR>
         Error Description: <%= objASPError.Description %> <BR>          
         Source: <%= objASPError.Source %><BR>
         Category: <%= objASPError.Category %><BR>
         Line Number: <%= objASPError.Line %><BR>
         File: <%= objASPError.File %><BR>          
         Column: <%= objASPError.Column %><BR>          
         ASP Code: <%= objASPError.ASPCode %><BR>
         ASP Description: <%= objASPError.ASPDescription %><hr>
    <%
    End if

    if isobject(objASPError) then
         set objASPError = Nothing
    end if    

Author

Commented:
Hi fritz the blank,
the error description I mentiond is for VBScript error not for connection error.
>>In the latest version of IIS,there is a better mechanism using the ASPError object.
could you tell me which version exactly.

Thanks
Lin
CERTIFIED EXPERT
Top Expert 2005

Commented:
My understanding is that you need ASP 3.0 for Server.GetLastError() to work.

In regards to getting the VBScript error, it may be that your connection error resulted in an additional VBScript error; perhaps you were trying to address some property or method that didn't exist as a result of the connection error.

Also, as I mentioned above, please make sure that you have changed objConnection to objcon to match your code. Also, it is necessary to call the error script before closing the connection or else you may not get any connection error information.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.