?
Solved

How do I open a PDF Blob field in SQL Server and open the file using VB.Net?

Posted on 2014-01-13
7
Medium Priority
?
1,058 Views
Last Modified: 2014-01-14
I have a SQL Server table that contains a BLOB column (PDF files are stored in this column). I need to retrieve it using VB.Net and open it as a PDF File. Any ideas?

Thanks!
0
Comment
Question by:BlakeMcKenna
[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
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:ktaczala
ID: 39778208
Open it as a filesteam, write it to disk as "c:\temp\tmpfile.pdf" then run process.start("c:\temp\tmpfile.pdf") that will open the file with the default PDF app.
0
 
LVL 12

Expert Comment

by:ktaczala
ID: 39778239
A little more detail.

load the blob into a datatable:
get the image.length into a variable
get the # of bytes of the image.
read it into a memorystream
write it to a filestream
 then open with process.start

for example (this is a guide)
        If ImageDataGridView.Rows.Count = 0 Then 'no row exists
            Exit Sub
        End If

        Dim curimageID = row.ImageID
        currentImageID = curimageID
        Dim _dtImage As New DSDocuments.ImageDataTable
        Dim _taImage As New DSDocumentsTableAdapters.ImageTableAdapter
        Dim _drImage As DSDocuments.ImageRow
        Dim _intLength As Integer
        Dim _imageBytes() As Byte
        Try
            _taImage.FillImagesByImageID(_dtImage, curimageID)
            If _dtImage.Count > 0 Then
                _drImage = _dtImage.Rows(0)
                _intLength = _drImage.Image.Length
                _imageBytes = _drImage.Image     ' database value into bytes
                Dim _originalImageMemoryStream As New System.IO.MemoryStream(_imageBytes, 0, _imageBytes.Length) ' bytes --> stream
                If Not Directory.Exists(tempfilepath) Then
                    Directory.CreateDirectory(tempfilepath)
                End If
                If File.Exists(tempfilepath & "\temp" & tempincrement.ToString & fileextension) Then
                    Try
                        Dim _fs1 As FileStream = File.OpenWrite(tempfilepath & "\temp" & tempincrement.ToString & fileextension)
                        _fs1.Close()
                        File.Delete(tempfilepath & "\temp" & tempincrement.ToString & fileextension)
                    Catch ex As Exception
                        tempincrement += 1
                    End Try
                End If
                viewercurrentfilename = tempfilepath & "\temp" & tempincrement.ToString & fileextension
                Dim _fs As FileStream = File.OpenWrite(viewercurrentfilename)
                _originalImageMemoryStream.WriteTo(_fs)
                _fs.Close()
                            System.Diagnostics.Process.Start(viewercurrentfilename)
0
 

Author Comment

by:BlakeMcKenna
ID: 39779290
Ok, I tried what you suggested in your 1st post. However, I get an error saying:

"A required privilege is not held by the client".

What is that? I'm writing the file to my C: drive and I'm the administrator of my own machine. Never seen this one before.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
ktaczala earned 2000 total points
ID: 39779306
save it to an existing folder not in the root
0
 

Author Comment

by:BlakeMcKenna
ID: 39779320
That worked but why can't I save it to the root drive?
0
 
LVL 12

Expert Comment

by:ktaczala
ID: 39779343
Windows(microsoft) just don't want you to save there by default.
0
 

Author Comment

by:BlakeMcKenna
ID: 39779476
That works for me!

Thanks again!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

762 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