Link to home
Start Free TrialLog in
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) ToutountzoglouFlag for Greece

asked on

PDF from SQL SERVER to Web Browser Control

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)
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John (Yiannis) Toutountzoglou

ASKER

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)?