Binary Writer generating empty files one one server but not on oth

F

flutophilus

I have a routine that generates a file (actually a pdf) from a database blob
object. It has been working for several years on a variety of servers and
PC's. Now we have one server on which the routine generates empty ( zero
byte) files. This server is running Windows 2003 R2 SP2 but we have another
server with the same configuration which runs exactly the same release and
works fine. I've tried writing the files to different locations including a
hard-code folder on the C: drive and the Application.CommonAppDataPath but it
makes no difference. I've tried runnin as loca machine\administrator but it
doesn;t work.

Here's the code (dbhelper is a standard function that returns an open
connection object)

Public Function FetchPDFtoFile(ByVal booTestMode As Boolean, ByVal strRecno
As String, ByVal strTempPath As String) As String

' Read the specified PDF and create a data file

Dim dbHelper As New dbHelper
' open database connection
Dim dcConnection As OdbcConnection =
dbHelper.GetODBCConnection(booTestMode)

Using dcConnection

' set up the query string depending on user-supplied data
Dim strCommand As String = "SELECT pdf FROM pdfdocument WHERE
recno = " & strRecno

' read data into the DataReader. NB Sequential Access parm
Dim cmCommand As New OdbcCommand(strCommand, dcConnection)
Dim drReaderPDF As OdbcDataReader =
cmCommand.ExecuteReader(CommandBehavior.SequentialAccess)

If drReaderPDF.Read() Then

' create random temp file name
Dim dtNow As New Date
dtNow = Now()
Dim intMilliSec As Integer = dtNow.Millisecond()
Dim intSec As Integer = dtNow.Second
Dim randObj As New Random(intMilliSec + intSec)
Dim strPdfFileName As String = "doc" & randObj.Next.ToString
& ".pdf"

Directory.CreateDirectory(strTempPath)
Dim strPdfFullFileName As String = strTempPath &
strPdfFileName

'NB these temp files should be removed in the global section
when app shuts
' down but just in case ...
File.Delete(strPdfFullFileName)

' Create a filestream to write the output to the temp file.
Dim fs As New FileStream(strPdfFullFileName,
FileMode.OpenOrCreate, FileAccess.Write)
Dim bw As New BinaryWriter(fs)

Dim bufferSize As Integer = 1000 ' The size of the BLOB
buffer.
Dim outbyte(bufferSize - 1) As Byte ' The BLOB byte()
buffer to be filled by GetBytes.
Dim retval As Long ' The bytes returned
from GetBytes.
Dim startIndex As Long ' The starting position
in the BLOB output.

' Reset the starting byte for a new BLOB.
startIndex = 0

' Read bytes into outbyte() and retain the number of bytes
returned.
retval = drReaderPDF.GetBytes(0, 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 = drReaderPDF.GetBytes(0, startIndex, outbyte, 0,
bufferSize)
Loop

If retval > 0 Then
' Write the remaining buffer.
bw.Write(outbyte, 0, CInt(retval - 1))
bw.Flush()
End If
' Close the output file.
bw.Close()
fs.Close()
dbHelper.CloseODBC(dcConnection)

Return strPdfFullFileName
Else
dbHelper.CloseODBC(dcConnection)
Return Nothing
End If
End Using

End Function
 
G

Gregory A. Beamer

I would find what is differnt about the two servers: installed software,
patches, etc. The fact the code is working on one server suggests that the
code is not the issue, or at least not the only issue. At a cursory glance I
see nothing wrong with it.

One thing I might add is some instrumentation to the code, like checking the
pulled blob and flagging when it has nothing to write as well as when it
writes a 0 byte file. This will help troubleshooting where in the pipeline
the problem is.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

*********************************************
| Think outside the box |
*********************************************
 
C

Colbert Zhou [MSFT]

Hello flutophilus,

Yes. The codes look all right. Besides Gregory's suggestions, may I ask the
following questions regarding to the issue,

1. Do we have a chance to do a line stepping debug? If yes, I think we need
to make sure all the codes are executed, as well as the pdf retrieved from
the database is not null. If we cannot, can we modify the codes to log the
application's executing status.
2. Have you tried to write some codes to test whether the StringWriter or
TextWriter works in that specified server?
3. If we use the Process Monitor and run our application. Does it monitor
and record something failed? To download the tool Process Monitor, please
visit http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx

We are looking forward your reply. Have a nice day!

Best regards,
Colbert Zhou (colbertz @online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Colbert Zhou [MSFT]

Hello flutophilus ,

I am writing to check the status of the issue on your side. Could you
please let me know if the suggestion heps to narrow down the problem? If
you need any future assistance, please feel free to let me know. I will be
more than happy to give follow up support.

Have a great day!

Best regards,
Colbert Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
F

flutophilus

Hi,

Thanks for the suggestions and the verification of the code. I've been off
the project for a month but am back on it now and will try implementing your
ideas and will report back.
 
C

Colbert Zhou [MSFT]

Hello,

Is there any update on this thread?


Best regards,
Colbert Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top