How to Upload and Read PDF File in VB.Net and MySQL Database

In this tutorial, I will teach you how to Upload and Read PDF File in VB.Net and MySQL Database. This Method will help you Upload the file in the database and view it into the Adobe PDF Reader.This is a step by step guide and it's very easy to learn. Before we start: Create a database named "test". Create a table in the database that you have created.
  1. CREATE TABLE `test`.`tblfiles` ( `FileId` INT NOT NULL AUTO_INCREMENT , `FileName`TEXT NOT NULL , PRIMARY KEY (`FileId`)) ENGINE = InnoDB;
Lets begin:

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application. After that, do the following form just like shown below. pdf1

Step 2

Add another form and add PDF Reader inside the form. pdf2 pdf3

Step 3

In the Form1, double click the Browse button to fire the click event handler and do the following code for finding the PDF file.
  1. Try
  2.             OpenFileDialog1.Filter = "PDF | *.pdf"
  3.             If OpenFileDialog1.ShowDialog = DialogResult.OK Then
  4.                 TextBox1.Text = OpenFileDialog1.FileName
  5.             End If
  6.         Catch ex As Exception
  7.             MsgBox(ex.Message)
  8.         End Try

Step 4

Set the imports above the Public Class.
  1. Imports MySql.Data.MySqlClient

Step 5

Initialize all the classes that are needed.
  1. Dim con As MySqlConnection = New MySqlConnection("server=localhost;user id=root;password=janobe;database=test;sslMode=none")
  2.     Dim cmd As MySqlCommand
  3.     Dim da As MySqlDataAdapter
  4.     Dim dt As DataTable
  5.     Dim sql As String

Step 6

Do the following code for retrieving data from the database to the datagridview.
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.         Try
  3.             con.Open()
  4.             sql = "SELECT * FROM tblfiles"
  5.             cmd = New MySqlCommand
  6.             With cmd
  7.                 .Connection = con
  8.                 .CommandText = sql
  9.             End With
  10.             da = New MySqlDataAdapter
  11.             dt = New DataTable
  12.             da.SelectCommand = cmd
  13.             da.Fill(dt)
  14.  
  15.             DataGridView1.DataSource = dt
  16.  
  17.         Catch ex As Exception
  18.             MsgBox(ex.Message)
  19.         Finally
  20.             da.Dispose()
  21.             con.Close()
  22.         End Try
  23.     End Sub

Step 7

Go back to the design view (Form1), double click the timer to fire the tick event handler of it and do the following code for uploading pdf file in the database.
  1. Try
  2.             ProgressBar1.Value += 1
  3.             If ProgressBar1.Value = 100 Then
  4.                 Timer1.Stop()
  5.  
  6.                 con.Open()
  7.                 sql = "INSERT INTO `tblfiles` (`FileName`) VALUES ('" & System.IO.Path.GetFileName(TextBox1.Text) & "')"
  8.  
  9.                 cmd = New MySqlCommand
  10.                 With cmd
  11.                     .Connection = con
  12.                     .CommandText = sql
  13.                     .ExecuteNonQuery()
  14.                 End With
  15.  
  16.                 If TextBox1.Text <> "" Then
  17.                     System.IO.File.Copy(TextBox1.Text, Application.StartupPath & "\PDF_Files\" & System.IO.Path.GetFileName(TextBox1.Text), True)
  18.                 End If
  19.                 MsgBox("Scanned file uploaded successfully.")
  20.  
  21.  
  22.                 TextBox1.Clear()
  23.                 ProgressBar1.Value = 0
  24.             End If
  25.  
  26.         Catch ex As Exception
  27.             MsgBox(ex.Message)
  28.         Finally
  29.             con.Close()
  30.         End Try
  31.         'call to retrieve file
  32.         Call Form1_Load(sender, e)

Step 8

Go back to the design view (Form1) again, double click the Upload button to fire click event handler and set the timer to start.
  1.     Timer1.Start()

Step 9

Double click the View button to fire the click event handler and do the following code to view the uploaded pdf file.
  1. Try
  2.             con.Open()
  3.             sql = "SELECT * FROM tblfiles WHERE FileId=" & DataGridView1.CurrentRow.Cells(0).Value
  4.             cmd = New MySqlCommand
  5.             With cmd
  6.                 .Connection = con
  7.                 .CommandText = sql
  8.             End With
  9.             da = New MySqlDataAdapter
  10.             dt = New DataTable
  11.             da.SelectCommand = cmd
  12.             da.Fill(dt)
  13.  
  14.             With Form2
  15.                 .Show()
  16.                 .Focus()
  17.                 .AxAcroPDF1.src = Application.StartupPath & "\PDF_Files\" & dt.Rows(0).Item("FileName")
  18.             End With
  19.         Catch ex As Exception
  20.             MsgBox(ex.Message)
  21.         Finally
  22.             da.Dispose()
  23.             con.Close()
  24.         End Try
Note: add mysql.data.dll for the references to access MySQL Library The complete sourcecode is included. You can download it and run it on your computer. For more question about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT FB Account – https://www.facebook.com/onnaj.soicalap

Add new comment