Link to home
Start Free TrialLog in
Avatar of Whing Dela Cruz
Whing Dela CruzFlag for Anguilla

asked on

ASP Error Handler

Hi experts, I am trying to implement some error handling on ASP but code below seems not working very well. When I update the data the "response.write("Successfully Updated!")" will appeared and this is what I want. However, after that, I Intentionally stop the mssql from running then click update again then the  response.write("Successfully enrolled new item!")  appeared. And when I click update again the "Response.Write( "1|Problems connecting to db.4s" )" was appeared. I observed that the code won't follow the right sequence. I'm hoping for help and ask some  insight why? Thank you!


<%
On Error Resume Next

dim pname,pcode,qty,um,reOr,dptid,tcode,uc,sp,pft

pname = request.querystring("a")
pcode = request.querystring("b")
qty = request.querystring("c")
um = request.querystring("d")
reOr = request.querystring("e")
dptid = request.querystring("f")
tcode = request.querystring("g")
uc = request.querystring("h")
sp = request.querystring("i")
pft = request.querystring("j")

response.expires=-1

Set cn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
cn.ConnectionString = "driver={SQL Server};server=pcsever;uid=;pwd=;DataBase=Mobile;"
cn.Open

If Err.Number <> 0 Then
    Response.Write( "1|Problems connecting to db.4s" )
    'Response.Write(  Err.Number & "|" & Err.Description )
    Response.End()
else
   Set rs = cn.Execute("Select Procode From Stock where Procode='" & pcode & "'")
           with rs
               If .BOF = True And .EOF = True Then
                  cn.execute("INSERT INTO Stock(Procode,Proname,Quantity,UnitCost,SellingPrice,Um,ReOrder,DPTID,TextCode) values('" & _
                     pcode & "','" & replace(pname, "'","''") & "'," & qty & "," & uc & "," & sp & ",'" & um & _
                     "'," & ReOr & ",'" & dptid & "','" & tcode & "')")
                  
                  response.write("Successfully enrolled new item!")
                  Response.End() 
                  
               Else
                  cn.execute("Update Stock Set Proname='" & pname & "', Quantity=" & qty & ", UnitCost=" & uc & ", SellingPrice=" & _
                     sp & ", Um='" & um & "', ReOrder ='" & reOr & "', DPTID='" & dptid & "', TextCode='" & tcode & "' Where Procode='" & pcode & "'")
                  response.write("Successfully Updated!")
                  On Error GoTo 0
                  response.End()
                  
               end if
           End with
   Set rs = nothing
   cn.close
end if
On Error GoTo 0
%>

Open in new window

SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Whing Dela Cruz

ASKER

Its working very find Ryan Chong, If you can tell me a little overview how this sequences happen it would be appreciated. Thank you, and more power to both of you sirs!
in general, since you have the line of code such as:

On Error Resume Next

it means when there is an error detected, your compiler will continue to run the following scrpt, but having said that, the error details were kept and we can refer it to the Err object, and therefore you can refer to Err.Number and Err.Description when necessary for error details.
On Error Resume Next
...
If Err.Number <> 0 Then
				      Response.Write(  Err.Number & "|" & Err.Description )
				  else
					  response.write("Successfully enrolled new item!")
				  end if	
...

Open in new window


if Err.Number is not equals to 0, it means there was an error captured.
Thanks a lot Ryan!