Solved

PDF from SQL SERVER to Web Browser Control

Posted on 2013-02-06
2
555 Views
Last Modified: 2013-02-06
I know how to upload and insert a pdf file to my sql  table...Here is the Code
 Private Sub UploadBuildRoolBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadBuildRoolBtn.Click
        Try
            Dim PdfFileDialog As New OpenFileDialog()
            PdfFileDialog.Filter = "pdf file|*.pdf"
            If PdfFileDialog.ShowDialog = System.Windows.Forms.DialogResult.OK Then

                Me.PdfLink.Text = PdfFileDialog.FileName

                WebBrowser1.Navigate(PdfFileDialog.FileName)

                Dim filebyte As Byte() = Nothing

                Dim PDFconn As New SqlConnection(ProjectConnectionString)
                Dim PDFcmd As SqlCommand = Nothing

                filebyte = System.IO.File.ReadAllBytes(PdfFileDialog.FileName)


                PDFcmd = New SqlCommand("Insert into Buildings ( PDFfile ) Values(@Mypdf)", PDFconn)
                PDFcmd.Parameters.Add("@Mypdf", SqlDbType.Binary).Value = filebyte
                PDFconn.Open()
                PDFcmd.ExecuteNonQuery()
                PDFconn.Close()
            End If
        Catch ex As Exception
              MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

Open in new window

The SQL data type is VARBINARY(Max)  ..
i need now to retrieve the this pdf back to my vb.net form inside a web browser
i know how to do it if the sql data type was Image via Memory stream
i do the same with this type but the Web Browser shows nothing Code....
 Try
            Dim PDFconn As New SqlConnection(ProjectConnectionString)
            Dim PdfSql As String = "SELECT PDFfile FROM Buildings WHERE Key_Building=12"
            Dim PdfCmd As New SqlCommand(PdfSql, PDFconn)
            If PDFconn.State = ConnectionState.Open Then PDFconn.Close()
            PDFconn.Open()
            Dim PDFReader As SqlDataReader = PdfCmd.ExecuteReader(CommandBehavior.CloseConnection)
            If PDFReader.HasRows Then
                While PDFReader.Read
                    Dim ms As MemoryStream = Nothing
                    Dim DataPDF() As Byte
                    DataPDF = PDFReader.Item("PDFfile")
                    ms = New MemoryStream(DataPDF)
                    WebBrowser1.DocumentStream = ms
                    ms.Close()
                End While
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Open in new window

What obviously thing am i missing?

John (Yiannis)
0
Comment
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 350 total points
ID: 38859559
You may have to change to

                 DataPDF = PDFReader.Item("PDFfile")
                 IO.File.WriteAllBytes("filepath", DataPDF)
                    WebBrowser1.Navigate("filepath")


Images can be interpreted by browser natively. But for PDF, it has to use a plugin.
0
 
LVL 18

Author Comment

by:John (Yiannis) Toutountzoglou
ID: 38859660
yes you are right...i did not know that there is a "recreation" of the file...Thank you once more..So in this case i have to create a temp path and then delete the file (after the load)?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

760 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

18 Experts available now in Live!

Get 1:1 Help Now