PDF Corrupt After Being Read from SQL 2000

Posted on 2005-04-22
Medium Priority
Last Modified: 2010-05-18
I have a database with PDFs stored on it. The app reads the PDF from the database and stores it on the machine as a temp file, then launches Acrobat Reader. At that point, I receive a message that the file format is invalid. I receive the same message when I have the app save the file as .pdf. I'm reading the PDF from the database using the Filestream object. Is there a way to prevent this?
Question by:Saney
  • 2
LVL 14

Accepted Solution

nayernaguib earned 350 total points
ID: 13846506
Are you sure that no data corruption has occured during the process of saving the PDFs to the database?


  Nayer Naguib

Author Comment

ID: 13847281
Here's the insert code:
fs = New System.IO.FileStream(txtLeasePDFFile.Text, IO.FileMode.OpenOrCreate, IO.FileAccess.Read)
        rawData = New Byte(fs.Length) {}
        fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length))
        ' Construct a SQL string and a connection object

        strSQL = "select * from Unit_PDF"
        sqlCon = New SqlClient.SqlConnection _
        Adapter = New SqlClient.SqlDataAdapter _
                    (strSQL, sqlCon)

        If sqlCon.State <> ConnectionState.Open Then
        End If
        cmbBuilder = New SqlClient.SqlCommandBuilder(Adapter)

        Adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

        DS = New Data.DataSet("Unit_PDF")
        ' Fill data adapter
        Adapter.Fill(DS, "Unit_PDF")

        ' Create a new row
        Row = DS.Tables("Unit_PDF").NewRow()
        Row("Unit_Num") = 1
        Row("Lease_PDf") = rawData

        ' Add row to the collection
        ' Save changes to the database
        Adapter.Update(DS, "Unit_PDF")

And here's the read code:
Dim strTempFile As String = Path.GetTempFileName
        ' Construct a SQL string and a connection object
        Dim strSQL As String = "SELECT Lease_PDf FROM Unit_PDF where " & _
            "Unit_Num=(select Unit_num from Unit_info where Tennant_name='" & _
            trLeaseProps.SelectedNode.ToString & "')"
        Dim sqlCon As SqlConnection = New _
        ' Open connection
        If sqlCon.State <> ConnectionState.Open Then
        End If

        strTempFile = Mid(strTempFile, 1, Len(strTempFile) - 3) & "pdf"
        Dim cmd As SqlCommand = New SqlCommand(strSQL, sqlCon)
        Dim fs As FileStream
        Dim bw As BinaryWriter
        Dim bufferSize As Integer = 300000
        Dim outbyte(300000 - 1) As Byte
        Dim retval As Long
        Dim startIndex As Long = 0
        Dim pub_id As String = ""
        Dim reader As SqlDataReader = _
        ' Read first record
        fs = New FileStream(strTempFile, _
        FileMode.OpenOrCreate, FileAccess.Write)
        bw = New BinaryWriter(fs)
        startIndex = 0
        retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
        ' Close the output file.

Author Comment

ID: 13888313
Nevermind. I discovered my answer. I was truncating the file.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question