2017-05-27 8 views
2

jQuery Ajax関数を使用して従来のASP/VBスクリプトを呼び出すページがあり、成功とエラーの結果をハンドラに指定できます。これらのハンドラは、データベースにレコードを挿入するためのSQLコードを実行するだけのASPスクリプトから応答を返します。重複したキー違反を示すSQLエラーがある場合は、以下のコードを使用して、生成されたエラーメッセージを友好的なものに置き換えます。私が発見したように、コードは "if conn.Errors.Count"行に到達しないので、これは機能しません。 SQLがエラーを生成すると、コードはすぐに "conn.Execute"行の行番号を含むエラー・メッセージと共に戻されます。これが私の望むことをする方法はありますか?Ajaxを使用してスクリプトを呼び出すときのVBScriptエラーの処理

set conn=CreateObject("ADODB.Connection") 
conn.Open ConnString 
conn.Execute "INSERT ... " (long statement omitted for readability) 
if conn.Errors.Count> 0 then 
    if instr(conn.Errors(0).Description, "duplicate key") > 0 then 
     Response.Write "Unable to add herb - code already exists" 
    else 
     Response.Write conn.Errors(0).Description 
    end if 
else ' success 
    Response.Write "Herb added" 
end if 
conn.Close 
set conn=nothing 
+1

を参照してください:[ASPでのエラー処理](http://www.4guysfromrolla.com/webtech/060399-1.shtml)[エラー時に」使用の – SearchAndResQ

+0

可能な重複ので、あなたのコードは次のようになります従来のASPの「次のレジューム」、エラーの処理方法](https://stackoverflow.com/questions/17445890/using-on-error-resume-next-in-classic-asp-and-how-to-handle-エラー) –

+0

こんにちは - 古典的なASPは、別のことを指定しない限り、予期せぬエラーが発生したときに終了します。あなたのケースでは、SQLによってエラーが発生します。したがって、終了します。エラー処理について話す方法として、try-catchについて聞いたことがあるかもしれません。従来のASPにはtry-catchがありません。代わりにエラーが発生した場合はnextを再開します。他の人が提供しているリンクを使用してください。 –

答えて

0

他の人も指摘しているように、最善の解決策は「次回エラー時に使用する」ことです。

on error resume next 
set conn=CreateObject("ADODB.Connection") 
conn.Open ConnString 
conn.Execute "INSERT ... " (long statement omitted for readability) 
if Err.Number > 0 then 
    if instr(Err.Description, "duplicate key") > 0 then 
     Response.Write "Unable to add herb - code already exists" 
    else 
     Response.Write conn.Errors(0).Description 
    end if 
else ' success 
    Response.Write "Herb added" 
end if 
conn.Close 
set conn=nothing 
on error goto 0 '-- this will remove error handling for the rest of the page and can be considered optional in this case 
関連する問題