开发者

How to check if an SQL query executed without error in ASP?

开发者 https://www.devze.com 2022-12-25 10:50 出处:网络
Here\'s my code: if Request.Form(\"authorize\") <> \"\" and request.form(\"delete\") <> \"true\" then

Here's my code:

if Request.Form("authorize") <> "" and request.form("delete") <> "true" then
    post_ids = Request.form("authorize")
    ids = split(post_ids, ",")

    For i = LBound(ids) to U开发者_如何学PythonBound(ids)
        sql = "update tbl_comments set authorized = 'true' where comment_id = " & ids(i)
        pageDB.execute(sql)
    Next

    message = "<div id=""succeed""><strong>Updated</strong>: Your comments have been approved.</div>"
end if

Instead of just setting "message" to the success message i'd like to do something along the lines of...

if(pageDB.execute(sql) was succesful) then
    message = "<div id=""succeed""><strong>Updated</strong>: Your comments have been approved.</div>"
else 
    message = "<div id=""error""><strong>Error</strong>: Your comments have not been approved.</div>"
end if


You need to put your pageDB.execute(sql) within a try catch block

Something like this

message = "<div id=""succeed""><strong>Updated</strong>: Your comments have been approved.</div>"
Try
   pageDB.execute(sql)
Catch ex as Exception
   message = "<div id=""error""><strong>Error</strong>: Your comments have not been approved.</div>"
End Try

As the comment under your question suggests though, you should NOT update a SQL database like this as you exposing your database to hacking (very easy hacking)

Consider instead using command parameters. Lots of stuff on the net about this.

Edit

Now that we're talking about classic ASP error handling isn't as easy but still possible. Its a bigger subject so I'd recommend having a look at this article.

http://www.15seconds.com/issue/990603.htm

What type of object is pageDB?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号