• We just launched and are currently in beta. Join us as we build and grow the community.

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

rebeldocc

Growth Hacker
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
55
Likes
13
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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.
break
Before we start:
Create a database named "test".
  1. Create

    database

    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.
2018-09-07_5.png

Step 2

Add another form and add PDF Reader inside the form.
2018-09-07_2.png

2018-09-07_4.png

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. DataGridView1.DataSource = dt

  15. Catch ex As

    Exception
  16. MsgBox(ex.Message)
  17. Finally
  18. da.Dispose()
  19. con.Close

    ()
  20. End

    Try
  21. 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. con.Open

    ()
  6. sql = "INSERT INTO `tblfiles` (`FileName`) VALUES ('"

    & System.IO.Path.GetFileName(TextBox1.Text) & "')"

  7. cmd = New

    MySqlCommand
  8. With

    cmd
  9. .Connection = con
  10. .CommandText = sql
  11. .ExecuteNonQuery()
  12. End

    With

  13. If

    TextBox1.Text <> ""

    Then
  14. System.IO.File.Copy(TextBox1.Text, Application.StartupPath & "\PDF_Files\"

    & System.IO.Path.GetFileName(TextBox1.Text), True

    )
  15. End

    If
  16. MsgBox("Scanned file uploaded successfully."

    )


  17. TextBox1.Clear()
  18. ProgressBar1.Value = 0
  19. End

    If

  20. Catch ex As

    Exception
  21. MsgBox(ex.Message)
  22. Finally
  23. con.Close

    ()
  24. End

    Try
  25. 'call to retrieve file
  26. 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. With

    Form2
  14. .Show()
  15. .Focus()
  16. .AxAcroPDF1.src = Application.StartupPath & "\PDF_Files\"

    & dt.Rows(0).Item("FileName"

    )
  17. End

    With
  18. Catch ex As

    Exception
  19. MsgBox(ex.Message)
  20. Finally
  21. da.Dispose()
  22. con.Close

    ()
  23. End

    Try

Note: add mysql.data.dll

for the references to access MySQL Library
break
The complete sourcecode is included. You can download it and run it on your computer.
break
break
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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top