Solved

PDF from SQL SERVER to Web Browser Control

Posted on 2013-02-06
2
623 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 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

628 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