Is there any examples on using Try and Catch statement in ASP??
Printable View
Is there any examples on using Try and Catch statement in ASP??
what are you trying to do with them ..?
edit: these are .Net stuff??
Anyway, if its error handling its basically ..
On Error Resume Next '// ERROR CHECKING
If err <> 0 Then '// ERROR OCCURRED
On Error Goto 0 '// Clear Errors
On Error Resume Next '// NEW ERROR CHECKING
I want to use Try and Catch to catch the error in my ASP code. Im trying to insert and update data into Access database. I want the program to throw exception instead of displaying the error page.
Here's my code:
<%
Dim adoCon
Dim rs
Dim strSQL
Set adoCon = Server.CreateObject("ADODB.Connection")
'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("db1.mdb")
'Set an active connection to the Connection object using DSN connection
'adoCon.Open "DSN=guestbook"
'Create an ADO recordset object
Set rs = Server.CreateObject("ADODB.Recordset")
<script type="text/javascript">
var txt=""
function message()
{
try
{
strSQL = "insert into course values ('"+request.form("dNAME")+"','"+request.form("cNAME")+"','"+request.form("cNO")+"');"
}
catch(err)
{
txt="error occured"
}
}
</script>
'Open the recordset with the SQL query
rs.Open strSQL, adoCon
%>
The connection code all work correctly except for the javascript. It will show error when I submit data. Maybe is the arrangement of the data has got problem.
Any idea?
The only Error checking for ASP is what I posted above.
Otherwise:
1-you have to open and close the ASP tags for javascript to work ..
2-you cant pass a javascript variable to ASP, only the other way around
Here is a way to do it ..
Code:<%
Option Explicit
Dim objCon, strSQL
Dim dNAME, cNAME, cNO
On Error Resume Next
dNAME = request.form("dNAME")
cNAME= request.form("cNAME")
cNO= request.form("cNO")
Set objCon = Server.CreateObject("ADODB.Connection")
objCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("./db1.mdb")
strSQL = "insert into course values ('" & dNAME & "','" & cNAME & "','" & cNO & "');"
objCon.Execute (strSQL)
Set objCon = Nothing
If err = 0 Then
Response.Write "Record Added"
Else
Response.Write "Error Occurred<br>" & err.Description
End If
%>