classic ASP Error Handling in Data-Access

Hi Guys
I am using vbscript/AsP/MSSQL 2005 on my website
Below is a simple dababase-access subroutine in an asp web-page.
I would really appreciate if  someone could add basic error handling to it.
Thank you
Dory

SUB Order_delete
Dim sqlstr
cust_no=session("cust_no")
set Objconn = Server.CreateObject("ADODB.Connection")
Objconn.open application("Mydb)
Set rs = Server.CreateObject("ADODB.Recordset")
set rs.activeconnection=objconn
sqlstr= "DELETE FROM  ord_save"
sqlstr=sqlstr & " WHERE ord_save.cust_no like '" & cust_no & "' "
rs.open sqlstr
set rs = nothing
objconn.close
set objconn=nothing
End Sub
dory550Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Basic ASP error handling uses the On Error Resume construct, something like:
SUB Order_delete

   Dim sqlstr
   cust_no=session("cust_no")

   On Error Resume Next

   set Objconn = Server.CreateObject("ADODB.Connection")
   Objconn.open application("Mydb)

   '// Check if there was an error opening the connection
   If Err.number <> 0 Then
      '// Do something with the error
   End

   Set rs = Server.CreateObject("ADODB.Recordset")
   set rs.activeconnection=objconn
   sqlstr= "DELETE FROM  ord_save"
   sqlstr=sqlstr & " WHERE ord_save.cust_no like '" & cust_no & "' "
   rs.open sqlstr

   '// Check if there was an error executing the query
   If Err.number <> 0 Then
      '// Do something with the error
   End If

   On Error Goto 0

   set rs = nothing
   objconn.close
   set objconn=nothing

End Sub

Open in new window

0
 
sybeConnect With a Mentor Commented:
> sqlstr= "DELETE FROM  ord_save"
> sqlstr=sqlstr & " WHERE ord_save.cust_no like '" & cust_no & "' "

Why "LIKE" and not "=" ?

And why use a recordset when you don't need one?

<%
objconn.execute sSQL, , 128
%>
is all you need to execute a statement and skip the recordset object.
0
 
dory550Author Commented:

Hi
Many thanks to Sybe for comments about improving my script
With reference to error-handling. I understand that basically i need first to identify the error and then  display a friendly message to the user.
I was wondering if someone could comment on that. And maybe provide some sample of a friendly messages.
thanks
Dory

0
 
sybeCommented:
Users are not very interested in what went wrong, but you should be. Users want to know that it gets fixed, and you want to know exactly what went wrong so you can fix it.

So the best thing is to log the exact error message to a place where you have access to, and tell the user that some error has occurred, that it has been logged, and that it will be fixed.
0
 
dory550Author Commented:
Hi Guys
Thank you for your help
0
All Courses

From novice to tech pro — start learning today.