error handling

Discussion in 'ASP General' started by Eugene Anthony, Jun 22, 2006.

  1. One problem with the code bellow is after this code

    conn.qDupUser p1,rs

    I added:

    set rs = nothing

    to test the error handling capability.

    What happen is the code bellow gets executed

    If rs(0) = 1 then
    response.write "<center><font class='error'>Error: Username is
    unavailable</font></center><br><br>"
    conn.close
    set conn = nothing
    else

    along with the code bellow

    If Err.number <> 0 then
    Response.Write "<center><font class='error'>" & Err.number & ":" &
    Err.Description & "</font></center><br>"
    end if
    on Error goto 0


    THE CODE:


    If request.queryString("Action") = 2 then
    p1 = Trim(request.form("username"))
    p2 = Trim(request.form("password"))
    p3 = Trim(request.form("type"))
    If (p1 <> "" and p2 <> "") AND (Len(p1)<=12 and Len(p2)<=12) then
    on error resume next
    set conn = Server.CreateObject("ADODB.Connection")
    conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    Server.MapPath("/db/upload/stelladb.mdb") & ";"
    set rs = Server.CreateObject("ADODB.Recordset")
    conn.qDupUser p1,rs
    set rs = nothing
    If rs(0) = 1 then
    response.write "<center><font class='error'>Error: Username is
    unavailable</font></center><br><br>"
    conn.close
    set conn = nothing
    else
    arParms = Array(p1,p2,p3)
    sql = "INSERT INTO Account([Username],[Password],[Type]) VALUES(?,?,?)"
    RunQueryString sql, arParms
    End if
    if Err.number <> 0 then
    Response.Write "<center><font class='error'>" & Err.number & ":" &
    Err.Description & "</font></center><br>"
    end if
    on Error goto 0
    Else
    response.write "<center><font class='error'>Error: Invalid username or
    password</font></center><br><br>"
    End if
    End if


    How do I solve the problem?

    Eugene Anthony

    *** Sent via Developersdex http://www.developersdex.com ***
    Eugene Anthony, Jun 22, 2006
    #1
    1. Advertising

  2. Eugene Anthony

    Mike Brind Guest

    Eugene Anthony wrote:
    > One problem with the code bellow is after this code
    >
    > conn.qDupUser p1,rs
    >
    > I added:
    >
    > set rs = nothing
    >
    > to test the error handling capability.
    >
    > What happen is the code bellow gets executed
    >
    > If rs(0) = 1 then
    > response.write "<center><font class='error'>Error: Username is
    > unavailable</font></center><br><br>"
    > conn.close
    > set conn = nothing
    > else
    >
    > along with the code bellow
    >
    > If Err.number <> 0 then
    > Response.Write "<center><font class='error'>" & Err.number & ":" &
    > Err.Description & "</font></center><br>"
    > end if
    > on Error goto 0
    >
    >
    > THE CODE:
    >
    >
    > If request.queryString("Action") = 2 then
    > p1 = Trim(request.form("username"))
    > p2 = Trim(request.form("password"))
    > p3 = Trim(request.form("type"))
    > If (p1 <> "" and p2 <> "") AND (Len(p1)<=12 and Len(p2)<=12) then
    > on error resume next
    > set conn = Server.CreateObject("ADODB.Connection")
    > conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    > Server.MapPath("/db/upload/stelladb.mdb") & ";"
    > set rs = Server.CreateObject("ADODB.Recordset")
    > conn.qDupUser p1,rs
    > set rs = nothing
    > If rs(0) = 1 then


    I'm not quite sure what your problem is, because you haven't exactly
    described it very well, but in the 4 lines above, you have declared a
    recordset object, opened it, set it to nothing and then tried to
    reference it.

    Obviously, since you have set rs = nothing, anything that belongs to rs
    is also set to nothing, including rs(0).

    --
    Mike Brind
    Mike Brind, Jun 22, 2006
    #2
    1. Advertising

  3. I purposely set rs = nothing

    to test the error handling capability and what happen is the code
    bellow gets executed

    If rs(0) = 1 then
    response.write "<center><font class='error'>Error: Username is
    unavailable</font></center><br><br>"
    conn.close
    set conn = nothing
    else

    followed by the code bellow

    If Err.number <> 0 then
    Response.Write "<center><font class='error'>" & Err.number & ":" &
    Err.Description & "</font></center><br>"
    end if
    on Error goto 0

    so I am getting two error msg which is wrong. Only one error msg is to
    be displayed.


    Eugene Anthony wrote:
    > One problem with the code bellow is after this code
    >
    > conn.qDupUser p1,rs
    >
    > I added:
    >
    > set rs = nothing
    >
    > to test the error handling capability.
    >
    > What happen is the code bellow gets executed
    >
    > If rs(0) = 1 then
    > response.write "<center><font class='error'>Error: Username is
    > unavailable</font></center><br><br>"
    > conn.close
    > set conn = nothing
    > else
    >
    > along with the code bellow
    >
    > If Err.number <> 0 then
    > Response.Write "<center><font class='error'>" & Err.number & ":" &
    > Err.Description & "</font></center><br>"
    > end if
    > on Error goto 0
    >
    >
    > THE CODE:
    >
    >
    > If request.queryString("Action") = 2 then
    > p1 = Trim(request.form("username"))
    > p2 = Trim(request.form("password"))
    > p3 = Trim(request.form("type"))
    > If (p1 <> "" and p2 <> "") AND (Len(p1)<=12 and Len(p2)<=12) then
    > on error resume next
    > set conn = Server.CreateObject("ADODB.Connection")
    > conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    > Server.MapPath("/db/upload/stelladb.mdb") & ";"
    > set rs = Server.CreateObject("ADODB.Recordset")
    > conn.qDupUser p1,rs
    > set rs = nothing
    > If rs(0) = 1 then
    > response.write "<center><font class='error'>Error: Username is
    > unavailable</font></center><br><br>"
    > conn.close
    > set conn = nothing
    > else
    > arParms = Array(p1,p2,p3)
    > sql = "INSERT INTO Account([Username],[Password],[Type]) VALUES(?,?,?)"
    > RunQueryString sql, arParms
    > End if
    > if Err.number <> 0 then
    > Response.Write "<center><font class='error'>" & Err.number & ":" &
    > Err.Description & "</font></center><br>"
    > end if
    > on Error goto 0
    > Else
    > response.write "<center><font class='error'>Error: Invalid username or
    > password</font></center><br><br>"
    > End if
    > End if
    >
    >
    > How do I solve the problem?
    >
    > Eugene Anthony
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    solomon_13000, Jun 23, 2006
    #3
  4. Eugene Anthony

    Mike Brind Guest

    On Error Resume Next tells the VBScript engine to ignore errors and
    continue with the next line of code. Details of the last error
    encountered are stored in the Err object. Previous error information
    is lost. On Error Goto 0 switches off On Error Resume Next, so code
    continues executing normally until it reaches another error.

    More information:
    http://blogs.msdn.com/ericlippert/archive/2004/08/19/217244.aspx

    --
    Mike Brind

    solomon_13000 wrote:
    > I purposely set rs = nothing
    >
    > to test the error handling capability and what happen is the code
    > bellow gets executed
    >
    > If rs(0) = 1 then
    > response.write "<center><font class='error'>Error: Username is
    > unavailable</font></center><br><br>"
    > conn.close
    > set conn = nothing
    > else
    >
    > followed by the code bellow
    >
    > If Err.number <> 0 then
    > Response.Write "<center><font class='error'>" & Err.number & ":" &
    > Err.Description & "</font></center><br>"
    > end if
    > on Error goto 0
    >
    > so I am getting two error msg which is wrong. Only one error msg is to
    > be displayed.
    >
    >
    > Eugene Anthony wrote:
    > > One problem with the code bellow is after this code
    > >
    > > conn.qDupUser p1,rs
    > >
    > > I added:
    > >
    > > set rs = nothing
    > >
    > > to test the error handling capability.
    > >
    > > What happen is the code bellow gets executed
    > >
    > > If rs(0) = 1 then
    > > response.write "<center><font class='error'>Error: Username is
    > > unavailable</font></center><br><br>"
    > > conn.close
    > > set conn = nothing
    > > else
    > >
    > > along with the code bellow
    > >
    > > If Err.number <> 0 then
    > > Response.Write "<center><font class='error'>" & Err.number & ":" &
    > > Err.Description & "</font></center><br>"
    > > end if
    > > on Error goto 0
    > >
    > >
    > > THE CODE:
    > >
    > >
    > > If request.queryString("Action") = 2 then
    > > p1 = Trim(request.form("username"))
    > > p2 = Trim(request.form("password"))
    > > p3 = Trim(request.form("type"))
    > > If (p1 <> "" and p2 <> "") AND (Len(p1)<=12 and Len(p2)<=12) then
    > > on error resume next
    > > set conn = Server.CreateObject("ADODB.Connection")
    > > conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    > > Server.MapPath("/db/upload/stelladb.mdb") & ";"
    > > set rs = Server.CreateObject("ADODB.Recordset")
    > > conn.qDupUser p1,rs
    > > set rs = nothing
    > > If rs(0) = 1 then
    > > response.write "<center><font class='error'>Error: Username is
    > > unavailable</font></center><br><br>"
    > > conn.close
    > > set conn = nothing
    > > else
    > > arParms = Array(p1,p2,p3)
    > > sql = "INSERT INTO Account([Username],[Password],[Type]) VALUES(?,?,?)"
    > > RunQueryString sql, arParms
    > > End if
    > > if Err.number <> 0 then
    > > Response.Write "<center><font class='error'>" & Err.number & ":" &
    > > Err.Description & "</font></center><br>"
    > > end if
    > > on Error goto 0
    > > Else
    > > response.write "<center><font class='error'>Error: Invalid username or
    > > password</font></center><br><br>"
    > > End if
    > > End if
    > >
    > >
    > > How do I solve the problem?
    > >
    > > Eugene Anthony
    > >
    > > *** Sent via Developersdex http://www.developersdex.com ***
    Mike Brind, Jun 23, 2006
    #4
  5. "solomon_13000" <> wrote in message
    news:...
    > I purposely set rs = nothing
    >
    > to test the error handling capability and what happen is the code
    > bellow gets executed
    >
    > If rs(0) = 1 then


    Earlier in your code you set On Error Resume Next.

    This line fails but that doesn't imply the whole If ... End If block is
    skipped. Code execution just falls into the 'Then' portion and executes
    that. I can't say whether it's guaranteed always to behave that way but at
    least in VBScript it fairly certain.

    > response.write "<center><font class='error'>Error: Username is
    > unavailable</font></center><br><br>"
    > conn.close
    > set conn = nothing
    > else
    >
    > followed by the code bellow
    >
    > If Err.number <> 0 then
    > Response.Write "<center><font class='error'>" & Err.number & ":" &
    > Err.Description & "</font></center><br>"
    > end if
    > on Error goto 0
    >
    > so I am getting two error msg which is wrong. Only one error msg is to
    > be displayed.
    >


    Frankly Error handling in VBScript stinks. Using a blanket On Error Resume
    Next can often to lead to all sorts of strange an seemingly inexplicable
    behaviour.

    If you really must use it extract the specific lines that really need this
    (there are usually only one or two lines that actually need this) and move
    them into their own function. You can place the On Error Resume Next in
    those functions.

    >
    > Eugene Anthony wrote:
    > > One problem with the code bellow is after this code
    > >
    > > conn.qDupUser p1,rs
    > >
    > > I added:
    > >
    > > set rs = nothing
    > >
    > > to test the error handling capability.
    > >
    > > What happen is the code bellow gets executed
    > >
    > > If rs(0) = 1 then
    > > response.write "<center><font class='error'>Error: Username is
    > > unavailable</font></center><br><br>"
    > > conn.close
    > > set conn = nothing
    > > else
    > >
    > > along with the code bellow
    > >
    > > If Err.number <> 0 then
    > > Response.Write "<center><font class='error'>" & Err.number & ":" &
    > > Err.Description & "</font></center><br>"
    > > end if
    > > on Error goto 0
    > >
    > >
    > > THE CODE:
    > >
    > >
    > > If request.queryString("Action") = 2 then
    > > p1 = Trim(request.form("username"))
    > > p2 = Trim(request.form("password"))
    > > p3 = Trim(request.form("type"))
    > > If (p1 <> "" and p2 <> "") AND (Len(p1)<=12 and Len(p2)<=12) then
    > > on error resume next
    > > set conn = Server.CreateObject("ADODB.Connection")
    > > conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    > > Server.MapPath("/db/upload/stelladb.mdb") & ";"
    > > set rs = Server.CreateObject("ADODB.Recordset")
    > > conn.qDupUser p1,rs
    > > set rs = nothing
    > > If rs(0) = 1 then
    > > response.write "<center><font class='error'>Error: Username is
    > > unavailable</font></center><br><br>"
    > > conn.close
    > > set conn = nothing
    > > else
    > > arParms = Array(p1,p2,p3)
    > > sql = "INSERT INTO Account([Username],[Password],[Type]) VALUES(?,?,?)"
    > > RunQueryString sql, arParms
    > > End if
    > > if Err.number <> 0 then
    > > Response.Write "<center><font class='error'>" & Err.number & ":" &
    > > Err.Description & "</font></center><br>"
    > > end if
    > > on Error goto 0
    > > Else
    > > response.write "<center><font class='error'>Error: Invalid username or
    > > password</font></center><br><br>"
    > > End if
    > > End if
    > >
    > >
    > > How do I solve the problem?
    > >
    > > Eugene Anthony
    > >
    > > *** Sent via Developersdex http://www.developersdex.com ***

    >
    Anthony Jones, Jun 23, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kenneth Keeley

    Error Handling

    Kenneth Keeley, Jul 1, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    380
    Kenneth Keeley
    Jul 1, 2003
  2. VSK
    Replies:
    1
    Views:
    779
    Alvin Bruney
    Jul 29, 2003
  3. Mark Tarver
    Replies:
    22
    Views:
    1,301
    J Kenneth King
    Apr 26, 2009
  4. Peter
    Replies:
    34
    Views:
    1,934
    James Kanze
    Oct 17, 2009
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    183
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page