
Ado Error in ASP
In Classic ASP, if you're working with ADO (ActiveX Data Objects) and want to handle or display errors, you can use the On Error Resume Next
statement along with the Err
object and optionally ADODB.Connection.Errors
.
? Basic Pattern: Catch ADO Errors in ASP
<%On Error Resume Next ' Enables error handlingDim conn, sqlSet conn = Server.CreateObject("ADODB.Connection")conn.Open "your_connection_string"sql = "SELECT * FROM NonExistentTable"conn.Execute sqlIf Err.Number <> 0 Then Response.Write "<b>ADO Error:</b><br>" Response.Write "Number: " & Err.Number & "<br>" Response.Write "Description: " & Err.Description & "<br>" Err.ClearEnd Ifconn.CloseSet conn = Nothing%>
? Detailed ADO Error Reporting (Connection.Errors
Collection)
If your connection object supports it (like with SQL Server), use this for more detailed ADO-specific error messages:
<%On Error Resume NextSet conn = Server.CreateObject("ADODB.Connection")conn.Open "your_connection_string"conn.Execute "SELECT * FROM BadTable"If Err.Number <> 0 Then Dim adoErr For Each adoErr In conn.Errors Response.Write "<b>ADO Error:</b><br>" Response.Write "Number: " & adoErr.Number & "<br>" Response.Write "Source: " & adoErr.Source & "<br>" Response.Write "Description: " & adoErr.Description & "<br><br>" Next conn.Errors.Clear Err.ClearEnd Ifconn.CloseSet conn = Nothing%>
? Tips
Always call
Err.Clear
andconn.Errors.Clear
after handling errors to reset the error state.conn.Errors
may contain multiple errors, especially with SQL Server.
? Example Output
ADO Error:Number: -2147217865Description: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'BadTable'.
Would you like a reusable HandleError
function or a way to log these to a file or database?