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