Whing Dela Cruz
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("Successfu lly 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("Successful ly 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
%>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
if Err.Number is not equals to 0, it means there was an error captured.
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
...
if Err.Number is not equals to 0, it means there was an error captured.
ASKER
Thanks a lot Ryan!
ASKER