I’m trying to read a binary file from a FileMaker 11 container field using Filemaker’s own ODBC driver. I was able to write files to the database and this works fine. retrieving them manually works fine and the files look OK and are not corupted.
However when retreiving them using VB.NET, and if the file size is approx > 5MB, I get the following “uncatchable” error (yes thats right, I cant “Try Catch End Try”, it just crashes):
System.AccessViolationException was unhandled
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Retrieving a file that is <5MB works fine.
Here is the code and where it crashes:
Using cn2 As New Odbc.OdbcConnection(G_AppSettings.ODBC_FileMaker("xxx", "xxx", "xxx")) ' Establish ODBC connection to FileMaker DB
cn2.Open()
cmd = New OdbcCommand("SELECT DocumentName, GetAs(DocumentContainer, 'FILE') FROM Documents WHERE DocumentID = " & id, cn2)
myReader = cmd.ExecuteReader()
If myReader.Read() Then
' get the name of the file
If Not myReader.IsDBNull(0) Then
TempDoc.FileName = myReader.GetValue(0)
End If
' check for problems:
If TempDoc.FileName = "" Then
MsgBox("Error: file name not specified. Could not open file.")
Exit Sub
End If
If tempDir = "" Then
MsgBox("Error: can't find local temp directory. Could not open file.")
Exit Sub
End If
' -----------------------------
' SAVE FILE IN TEMP WINDOWS DIR
' -----------------------------
fs = New FileStream(tempDir & "\" & TempDoc.FileName, FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
' Read bytes into outbyte() and retain the number of bytes returned.
Dim ds1 = myReader.GetDataTypeName(1)
Dim ds2 = myReader.GetFieldType(1)
Dim bytesRead = myReader.GetBytes(1, 0, outbyte, 0, bufferSize) < - CRASHES HERE
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
' Continue reading and writing while there are bytes beyond the size of the buffer.
Do While retval = bufferSize
bw.Write(outbyte)
bw.Flush()
' Reposition the start index to the end of the last buffer and fill the buffer.
startIndex += bufferSize
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
Loop
' Write the remaining buffer.
bw.Write(outbyte, 0, retval - 1)
bw.Flush()
' Close the output file.
bw.Close()
fs.Close()
End If
cn2.Close()
End Using
I am using Windows XP/7 clients and hosting the database on FileMaker Advanced Server 11.
Any help on this would be great!
After lots of trial and error, I have finally answered my own question. All of the code posted above works correctly, except I forgot to specify the behaviour for the OdbcDataReader
This line:
Should be:
It seems that this was causing the problems I was having where some files would open correctly while others wouldn’t. Hope this helps someone! Tim.