TL;DR How can I issue commands to reliably log FoxPro errors to a plain text file when running INSERTs and APPEND FROM commands through ODBC/OLE DB?
I am attempting to write some massaged data back into a FoxPro database. The actual connection to the .dbf's are done by a service written by a colleague - I'm assuming it uses ODBC or OLE DB. I only supply the path to the DBF and the query strings to run. However I am running into some problems with FoxPro, and unfortunately all my experience lies in SQL Server and friends - not FoxPro.
My problem is that simple statements like the INSERT below work fine in Visual Fp, but fail going through the service. I don't get much useful error information back other than "An error occured".
INSERT INTO "ADDRESSES" (addressid, firstname, lastname) ;
VALUES (55, 'Test', 'Testerson')
To make life easier for myself I have tried to issue more commands to make FoxPro log all error info to a text file. Again, this works in Visual FoxPro - but fails silently and does not output a file when run through the service. I've ensured the directory permissions are correct:
LOCAL lcOldOnError
lcOldOnError = ON("ERROR") && save old error handler
SET PROCEDURE TO [D:\foxpro\errhandler.prg]
ON ERROR DO ErrHandler WITH ERROR(), MESSAGE()
&& This insert causes a duplicate primary key error:
INSERT INTO "address book!addresses" (addressid, firstname, lastname) ;
VALUES (1, 'MyName', 'IsError')
ON ERROR &lcOldOnError && restore old error handler
The external PRG errorhandler.prg looks like this. It works inside Visual FoxPro:
PROCEDURE ErrHandler
PARAMETERS gnError, gcMsg
LOCAL aErrInfo[1]
AERROR(aErrInfo)
cErrMsg = LTRIM(STR(aErrInfo[1])) + CHR(13) + CHR(10) + aErrInfo[2] + CHR(13) + CHR(10) + aErrInfo[3] + CHR(13) + CHR(10)
lnFileHandle = FCREATE ([D:\foxpro\errorlog.txt])
FPUTS(lnFileHandle, cErrMsg)
FCLOSE(lnFileHandle)
ENDPROC
Any hints or tips would be appreciated, thanks.
@DRapp, here is my C# code. It is very site specific I am afraid, so don't know if it helps anything:
static void Main(string[] args)
{
// Test - not production code!
var errormessage = string.Empty;
DataSvc dataService = new DataSvc();
dataService.AppDataPath = @"D:\foxpro\test\DATA\addresses.dbf";
const string commandText = "INSERT INTO [address book!addresses] (addressid, firstname, lastname) VALUES (90, [Iam], [Error])";
var returnValue = dataService.ExecuteCommands(commandText, ref errormessage);
Console.WriteLine("\nResult:{0}\nErrorMsg: {1}", returnValue, errormessage);
Console.ReadLine();
}
I only get errors of the kind "An error occured" from DataSvc, so that's why I would like to decorate the INSERT statements with some more commands for rerouting error info to a text file.