Results 1 to 4 of 4

Thread: ASP: Try and Catch

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    108

    ASP: Try and Catch

    Is there any examples on using Try and Catch statement in ASP??

  2. #2
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ASP: Try and Catch

    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
    Last edited by rory; Jun 26th, 2006 at 02:11 AM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    108

    Re: ASP: Try and Catch

    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?

  4. #4
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: ASP: Try and Catch

    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
    
    %>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width