Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I want to show pdf file on vb.net form which is actually store into the database in binary format in my sql, i can't get it.. can any body know, how to do that?
Posted
Comments
Manas Bhardwaj 10-May-11 6:49am    
no effort!
Bodhani 10-May-11 7:15am    
thanks for your comment, but i tried with web browser and active-x adobe reader control also, and i successfully retrived file into byte array but fail to diplay on any of these control.

Thanks again.
Simon_Whale 10-May-11 17:45pm    
Update your question with the code that is giving you the issue and we will help as much as we can
Sandeep Mewara 10-May-11 6:55am    
Tried anything? Edit the question and update with your effort.

A quick and dirty way is just to put a WebBrowser control onto your form, and then use

C#
WebBrowser1.Navigate("Your pdf doc file");
.

Of course this presumes that IE and also a pdf reader is installed on the target machine.
 
Share this answer
 
Comments
Bodhani 10-May-11 7:28am    
Hello Wayne Gaylard,
i tried with webbrowser control, as i mentioned above my file actually stored into the database in blob field, and with active-x control it gives me an unhandle argument exception at the time bind bytes of array to the active-x control.

Thanks.
ok Then
Just Try the Following
i assume that You have Successfully Uploaded Your Pdf File in Your
Database,Data Type As Binary ..you can also specify the DataType as Image.Anyways may be i am too Late to Respond to your Q.As I too Was Struggling with this and Finally Got it

First Of all Confirm That Adobe Reader is installed in your System
We Will use be using The ShellEx library Function to open the associated File
Type. For here take it as Pdf File.
Declare The Following
Private Declare Function ShellEx Lib "shell32.dll" Alias "ShellExecuteA" ( _
  ByVal hWnd As Integer, ByVal lpOperation As String, _
  ByVal lpFile As String, ByVal lpParameters As String, _
  ByVal lpDirectory As String, ByVal nShowCmd As Integer) As Integer

   Dim SConnectionString As String = "Data Source=.\sqlexpress;Initial Catalog=New1;Integrated Security=True;Pooling=False"

   Dim connection As SqlConnection

Change the Connection String to your preferred one.
I have Used a Grid View To get the Related data
and Placed a View Button To open the File.So Change It as Per your need
Private Sub GetImagesFromDatabase()
        Try


            If connection.State = ConnectionState.Closed Then
                connection.Open()
            End If

            Dim strSql As String = "Select FileId,FileName," & _
            "FileType from FileStore"


            Dim ADAP As New SqlDataAdapter(strSql, connection)
            Dim DS As New DataSet()
            ADAP.Fill(DS, "FileStore")
            dbGridView.DataSource = DS.Tables("FileStore")
            'Adding Columns
            Dim dgButtonColumn As New DataGridViewButtonColumn
            dgButtonColumn.HeaderText = ""
            dgButtonColumn.UseColumnTextForButtonValue = True
            dgButtonColumn.Text = "View File"
            dgButtonColumn.Name = "ViewFile"
            dgButtonColumn.ToolTipText = "View File"
            dgButtonColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader
            dgButtonColumn.FlatStyle = FlatStyle.System
            dgButtonColumn.DefaultCellStyle.BackColor = Color.Gray
            dgButtonColumn.DefaultCellStyle.ForeColor = Color.White
            dbGridView.Columns(0).Visible = False
            dbGridView.Columns.Add(dgButtonColumn)


        Catch ex As Exception
            MessageBox.Show(ex.ToString())
            MessageBox.Show("Could not load the Image")
        End Try
    End Sub


In the Form Load Call the GetImagesFromDatabase function to fill up the Grid
and change the Fields as per your need
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      GetImagesFromDatabase()
    
  End Sub

Add the Following Function To Extract the File
Private Sub downLoadFile(ByVal iFileId As Long, ByVal sFileName As String, ByVal sFileExtension As String)
        Dim strSql As String

        Try

            strSql = "Select Data from FileStore WHERE FileId=" & iFileId

            Dim sqlCmd As New SqlCommand(strSql, connection)

            'Get image data from DB
            Dim fileData As Byte() = DirectCast(sqlCmd.ExecuteScalar(), Byte())

            Dim sTempFileName As String = Application.StartupPath & "\" & sFileName

            If Not fileData Is Nothing Then
                'Read image data into a file stream 
                Using fs As New FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Write)
                    fs.Write(fileData, 0, fileData.Length)
                    'Set image variable value using memory stream. 
                    fs.Flush()
                    fs.Close()
                End Using

            
                ShellEx(Me.Handle, "Open", sFileName, "", "", 10)
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub


And then on GridView Cell Content Click Event Call The Function
Dim strSql As String = "";
        If sender.Columns(e.ColumnIndex).Name = "ViewFile" Then
            downLoadFile(dbGridView.Rows(e.RowIndex).Cells("FileId").Value, dbGridView.Rows(e.RowIndex).Cells("FileName").Value, dbGridView.Rows(e.RowIndex).Cells("FileType").Value)
        End If



Change the Filed Names as Per your need
try this
it will work
For More Information on ShellEx Library
Please Visit the MSDN Site
 
Share this answer
 
Comments
Member 1293561 2-Aug-11 3:48am    
Hi! My name is Johnny.
My PC Spec: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz, 4GB RAM (2.74GB Usable), Windows 7 Ultimate
, VB.NET 2010 & SQL Server 2008 Express R2.
I had a Filesrtream Table and i had catalog refer to that table too.

Q1:
I able to store a pdf file with 466MB file size into FileStream Table.
I facing some problem to extract back the file.

Error at:
Dim fileData As Byte() = DirectCast(sqlCmd.ExecuteScalar(), Byte())

Error Message:
Exception of type 'System.OutOfMemoryException' was thrown.


Then i try to store other pdf file (582MB) and i get error:

ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context 0x4e7fc8 to COM context 0x4e8138 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

May i know what is happening and how to solve all this big file size storing and extract problem?

Q2:
SQL Memory didn't release... after storing file in filestream table.
May be you can try to evaluate like me. (Test with big file size like 80 - 100MB)
1. Open your task Manager check the sqlservr.exe there how much memory being used.
2. Store 1 file and go back task manager and check again
3. Store other file and check task manager again.
You sill see the memory is increase and didn't released.

I found out this in the Task Manager.

For SQL Memory didn't release i try to use DBCC FREESYSTEMCACHE but it didn't work at all.

In my research most people said increase your RAM. But i know this is not a solution for permanent.

Thank!

Regards
Johnny

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900