Jump to content
Sign in to follow this  
xavierh

ObjEvent does not fire on com error

Recommended Posts

xavierh

when a database query returns no recors, RecordSet.MoveFirst or MoveLast will generate an error.

I am workiing with SQLCE. The Recordset.RecordCount always returns -1. So trapping the error with MoveFirst is essential if the current query returns no records.

Here is what I'm attempting:

Dim $g_eventerror = 0
Dim $oMoveFirstError
Func RunCEQuery ( $Query, $DBFile, $bReturnColumns = False )
Dim $Ret = ""
Dim $sqlCon = ObjCreate("ADODB.Connection")
Dim $oRS = ObjCreate ( "ADODB.Recordset" )
$sqlCon.Open("Provider=Microsoft.SQLSERVER.CE.OLEDB.4.0;Data Source=" & $DBFile)
$oRS.open ( $Query, $sqlCon, 3, -1)
$oMoveFirstError = ObjEvent ( "AutoIT.Error", "Errhandler" ) ; SHOULD TRAP ERROR
$oRS.MoveFirst  ; CRASH OCCURS HERE if $oRS has no records
If $g_eventerror then
  $g_eventerror = 0
  Msgbox (0,"AutoItCOM test","Error was caught: " & @error)
Endif
etc. etc.
EndFunc
 
Func Errhandler()
Msgbox(0,"SQLCETool","COM Error !"    & @CRLF  & @CRLF & _
  "err.description is: "    & @TAB & $oMoveFirstError.description   & @CRLF & _
  "err.windescription:"  & @TAB & $oMoveFirstError.windescription & @CRLF & _
  "err.number is: "      & @TAB & hex($oMoveFirstError.number,8)  & @CRLF & _
  "err.lastdllerror is: "   & @TAB & $oMoveFirstError.lastdllerror   & @CRLF & _
  "err.scriptline is: "  & @TAB & $oMoveFirstError.scriptline    & @CRLF & _
  "err.source is: "      & @TAB & $oMoveFirstError.source        & @CRLF & _
  "err.helpfile is: "      & @TAB & $oMoveFirstError.helpfile      & @CRLF & _
  "err.helpcontext is: "    & @TAB & $oMoveFirstError.helpcontext _
)
Local $err = $oMoveFirstError.number
If $err = 0 Then $err = -1
$g_eventerror = $err
EndFunc

The Errhandler() function does not fire, and the script crashes on the $oRS.MoveFirst line if the recordset is empty, with this error: ==> The requested action with this object has failed.:.

Am I missing something?

Thanks,

Xavier

Edited by xavierh

Share this post


Link to post
Share on other sites
UEZ

Move

$oMoveFirstError = ObjEvent ( "AutoIT.Error", "Errhandler" ) ; SHOULD TRAP ERROR

to line 1 and it should work!

Reason is that the error handle isn't set before the ObjCreate()!

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
enaiman

IMO you need to register the error handler outside any function (in the main script) because outside your RunCEQuery function, $oMoveFirstError does not exist.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
xavierh

Thanks for taking the time to make suggestions.

UEZ, I moved the ObjEvent line up to before the ObjCreate statement, but that did not help. Besides, in the sample code that goes with the Function Reference help, the ObjEvent is after the ObjCreate, and immediately before the risky statement.

Mass Spammer, I do have a Dim $oMoveFirstError statement outside of the functions, it's the 2nd line in the code above. It does exist outside the RunCEQuery function.

So I'm still scratching my head, but that does not seem to help either.

Share this post


Link to post
Share on other sites
BrewManNH

Mass Spammer, I do have a Dim $oMoveFirstError statement outside of the functions, it's the 2nd line in the code above. It does exist outside the RunCEQuery function.

So I'm still scratching my head, but that does not seem to help either.

Shouldn't this $oMyError.description be this $oMoveFirstError.description?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
xavierh

MP,

you're right, it should, it's a copy paste from the Function Reference sample code, and I should have paid more attention to what it contained. I just rectified it. In my actual code I just have a msgbox statement to let me know that the ErrHandler function was actually called.

Share this post


Link to post
Share on other sites
xavierh

of course there is the easy way of avoiding the problem, just adding the line

if $oRS.EOF And $oRS.BOF Then Return ""

just before

$oRS.MoveFirst

But still the event should fire. Possible bug?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×