Solved

PDF from SQL SERVER to Web Browser Control

Posted on 2013-02-06
2
572 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

895 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