PDF Corrupt After Being Read from SQL 2000

Posted on 2005-04-22
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
    LVL 14

    Accepted Solution

    Are you sure that no data corruption has occured during the process of saving the PDFs to the database?


      Nayer Naguib

    Author Comment

    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

    Nevermind. I discovered my answer. I was truncating the file.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Title # Comments Views Activity
    post4 challenge 28 70
    copyEvens challenge 6 46
    bunnyEars challenge 6 45
    Path of Workbook 3 30
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    This is about my first experience with programming Arduino.
    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 …

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now