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
913 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
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:ktaczala
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Accepted Solution

by:
ktaczala earned 500 total points
Comment Utility
save it to an existing folder not in the root
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:BlakeMcKenna
Comment Utility
That works for me!

Thanks again!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now