• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

PDF Corrupt After Being Read from SQL 2000

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?
  • 2
1 Solution
Are you sure that no data corruption has occured during the process of saving the PDFs to the database?


  Nayer Naguib
SaneyAuthor Commented:
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.
SaneyAuthor Commented:
Nevermind. I discovered my answer. I was truncating the file.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now