John (Yiannis) Toutountzoglou
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
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....
John (Yiannis)
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
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
What obviously thing am i missing?John (Yiannis)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER