ODBC throws exception when reading a blank memo column from MS-Access database
Original Reporter info from Mantis: Thorsten Schmidt
-
Reporter name:
Original Reporter info from Mantis: Thorsten Schmidt
- Reporter name:
Description:
Symptom:
With an ODBC connection to an Access database an exception is thrown when trying to get the data from a memo field, which holds an empty string. Data should be shown i.e. in a dbgrid.
Details:
The exception occurs in TODBCConnection.LoadBlobIntoBuffer on reading the memo contents. The Result from SQLGetData is SQL_NO_DATA and the following call to ODBCCheckResult raises the exception. The previous call within TODBCConnection.LoadField figured cout correctly the field not SQL_NULL_DATA and it's size is zero. Therefore the result SQL_NO_DATA in LoadBlobIntoBuffer is also correct, as there are no (more) data to read.
Possible fix:
In TODBCConnection.LoadBlobIntoBuffer added an if clause to filter out the SQL_NO_DATA result.
begin
ODBCCursor:=cursor as TODBCCursor;
// Try to discover BLOB data length
// NB MS ODBC requires that TargetValuePtr is not nil, so we supply it with a valid pointer, even though BufferLength is 0
Res:=SQLGetData(ODBCCursor.FSTMTHandle, FieldDef.Index+1, SQL_C_BINARY, @BlobBuffer, 0, @StrLenOrInd);
if Res = SQL_NO_DATA then // added
StrLenOrInd := 0 // added
else // added
ODBCCheckResult(Res, SQL_HANDLE_STMT, ODBCCursor.FSTMTHandle, 'Could not get field data for field "%s" (index %d).',[FieldDef.Name, FieldDef.Index+1]);
// Read the data if not NULL
if StrLenOrInd<>SQL_NULL_DATA then
....
Steps to reproduce:
compile and run the attached test program. The included database contains three rows. One with memo null, one with memo text and one with a memo containing an emty string.
Mantis conversion info:
- Mantis ID: 25690
- OS: Windows
- OS Build: Win7
- Platform: amd64
- Version: 2.6.2
- Fixed in version: 3.0.0
- Fixed in revision: 23049 (#9e17ba41)
- Target version: 2.6.4