Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PDF from SQL SERVER to Web Browser Control

Posted on 2013-02-06
2
Medium Priority
?
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1400 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

722 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