Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Write an Error Handler for a Specific Error?

Status
Not open for further replies.

peggasus88

Technical User
Feb 25, 2002
28
0
0
US
Hi,

I purposely entered a primary key value that already existed in my database table and as expected, I received the error below. I would like to pop up an alert box to the User, rather than have them redirected to such a scary error page. =) I would like my alert box to be specific and pop up when a specific error occurs (Violation of PRIMARY KEY contraint...), and not whenever any type of error occurs. Can someone please show me how to do this?


Microsoft OLE DB Provider for ODBC Drivers error '80004005'

[Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PK_TestCardInfo'. Cannot insert duplicate key in object 'TestCardInfo'.


Thank you for any help you can give,

~*Peggy*~
 
Hi,

1. First I execute some client-side validation (blanks, format) For Example:

<!-- Linked Javascipt file contains general type functions such as Trim -->
<script type='text/javascript' src='Javascript_functions.js'></script>

<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
/////////////////////////////////////////////////////////////
// This function is used to validate data on the
// client before it is submitted to the server to ensure
// columns that do not allow nulls do not get submitted to server
//////////////////////////////////////////////////////////////
function thisPage_onbeforeserverevent( obj, event ){
if (obj==&quot;btsave&quot;)
{
if(event==&quot;onclick&quot;)
{ // Check if something entered for Stock Name
// Note: There is no javascript Trim function...
// See Javascript_functions.js
if(Trim(document.thisForm.txtstockname.value) == &quot;&quot;)
{
alert(&quot;Stock Name cannot be blank!&quot;);
// Cancel the update.
thisPage.cancelEvent = true;
document.thisForm.txtstockname.focus();
}

}

}

}
//-->;
</Script)

2. Next after a SQL Delete, update, insert I check the
ERR object. Example:

Sub btDelete_onclick()

' Turn OFF Error Handling
On Error Resume Next

' Delete record
rsStock.deleteRecord()

' Check if there was problem...could fail due to
' a Foreign Key constraint
CheckForError()

' If Deletion was successful move to last record
if Err.number = 0 then
' Deletion was successful
rsStock.moveLast()
else
' Deletion failed, return to previous state
rsStock.cancelUpdate()
DisableButtons()
end if

' Set Errors back on
On Error Goto 0
End Sub

3. CheckForError() is a VBScript function which is located within a linked VBScript general functions file. You could go further an but a case statement with all the most common errors. (I plan to do this.) Code:

<SCRIPT ID=serverEventHandlersVBS LANGUAGE=vbscript RUNAT=Server>

Function CheckForError()
'''''''''''''''''''''''''''''''''''''''''''''''''''''
' Note: The page this is embedded MUST have the following fields
' defined: txterrormsg, and btnerror
' Purpose: To check for Database errors after a database
' call
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if Err.number <> 0 Then
if Err.number = -2147217873 then
' Note: Error -2147217873 is a Foreign Key error
StatusMsg = &quot;Record cannot be deleted as it used as part of another definition. DB Error: &quot; & err.number
else
StatusMsg = &quot; Operation Failed: &quot; & Err.description & &quot; Err.number: &quot; & err.number
end if
txterrormsg.value = StatusMsg
txterrormsg.show
btnerror.show
End if
End Function


4. On my page I define 2 hidden DTC (design time controls)
txterrormsg (textarea field)
btnerror (button which when pressed enables any disabled edit buttons etc.)

I hope this helps...

Computergreek
 
Hi everyone,

Thanks for the helpful input.

Can someone direct me to a resource that has a list of errors and their error numbers? Or maybe a way of determining the error number? I don't believe the error number would be '80004005' for my 'Violation of PRIMARY KEY contraint' since I always seem to see that number when something goes wrong. I was searching the 4guysfromrolla site and an article mentioned that the error numbers may be different for different databases. I'm using SQL Server.

Thanks again.
 
Hi,

Check out MSDN Library article Q209050. (Select entire subset for your search.) This article lists the error message numbers and text that are returned from ADOfiltr.dll as a result of calling Desktoptodevice etc. within MS Visual Basic... I think this would be the same list as for MS Interdev...? If you find something different, please post your findings. Thanks.

Computergeek
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top