How to Update an Image in Access Database Using VB.Net

This time, I will teach you how to Update an Image in Access Database Using VB.net. I came up with this idea because this is a very important function when you are dealing with the database object. This method has the ability to update the stored image in the access database. It is also has a function that can view an image in the large view. It is a simple procedure that you can do even if you are a beginner in programming. Just follow the step by step guide to see how it works.

Creating an Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in visual basic. ps1

Step 2

Do the form just like shown below. ps2

Step 3

Press F7 to open the code editor. In the code editor, add a namespace for OLeDB to access OLeDB libraries.
  1. Imports System.Data.OleDb

Step 4

Create a connection between the access database and c#. After that, declare all the classes and variables that are needed.
  1.    
  2.    Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &
  3.                                                      Application.StartupPath & "\dbsaveimg.accdb")
  4.     Dim cmd As OleDbCommand
  5.     Dim da As OleDbDataAdapter
  6.     Dim dt As DataTable
  7.     Dim sql As String

Step 5

Create a method for retrieving images in the database.
  1.  
  2.     Private Sub loadData(sql As String, dtg As DataGridView)
  3.         Try
  4.             con.Open()
  5.  
  6.             cmd = New OleDbCommand
  7.             da = New OleDbDataAdapter
  8.             dt = New DataTable
  9.  
  10.             With cmd
  11.                 .Connection = con
  12.                 .CommandText = sql
  13.             End With
  14.  
  15.             da.SelectCommand = cmd
  16.             da.Fill(dt)
  17.  
  18.             dtg.DataSource = dt
  19.             dtg.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
  20.             dtg.Columns(0).Visible = False
  21.  
  22.         Catch ex As Exception
  23.             MsgBox(ex.Message)
  24.         Finally
  25.             con.Close()
  26.             da.Dispose()
  27.         End Try
  28.     End Sub

Step 6

Create a method for updating an image in the database.
  1.    
  2.     Private Sub executeQuery(sql As String)
  3.         Try
  4.             Dim arrImage() As Byte
  5.             Dim mstream As New System.IO.MemoryStream()
  6.  
  7.             'SPECIFIES THE FILE FORMAT OF THE IMAGE
  8.             PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
  9.  
  10.             'RETURNS THE ARRAY OF UNSIGNED BYTES FROM WHICH THIS STREAM WAS CREATED
  11.             arrImage = mstream.GetBuffer()
  12.  
  13.             'GET THE SIZE OF THE STREAM IN BYTES
  14.             Dim FileSize As UInt32
  15.             FileSize = mstream.Length
  16.             'CLOSES THE CURRENT STREAM AND RELEASE ANY RESOURCES ASSOCIATED WITH THE CURRENT STREAM
  17.             mstream.Close()
  18.  
  19.             con.Open()
  20.  
  21.             cmd = New OleDbCommand
  22.             With cmd
  23.                 .Connection = con
  24.                 .CommandText = sql
  25.                 .Parameters.AddWithValue("@img", arrImage)
  26.                 .ExecuteNonQuery()
  27.             End With
  28.  
  29.         Catch ex As Exception
  30.             MsgBox(ex.Message)
  31.         Finally
  32.             con.Close()
  33.             da.Dispose()
  34.         End Try
  35.     End Sub

Step 7

Write the following code for resizing an image in the first load of the form.
  1.  
  2.     Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
  3.  
  4.         If (e.RowIndex < 0 Or e.ColumnIndex < 0) Then Return
  5.  
  6.         If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ValueType = GetType(Byte()) Then
  7.             CType(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex),
  8.               DataGridViewImageCell).ImageLayout = DataGridViewImageCellLayout.Zoom
  9.         End If
  10.  
  11.     End Sub

Step 8

Write the following code for retrieving an image in the first load of the form
  1.    
  2.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  3.         sql = "Select ID,img as Picture From tblimage"
  4.         loadData(sql, DataGridView1)
  5.     End Sub

Step 9

Write the following code for passing the image from the datagridview into the picture box.
  1.    
  2.     Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  3.  
  4.         Dim arrImage() As Byte
  5.         arrImage = DataGridView1.CurrentRow.Cells(1).Value
  6.         Dim mstream As New System.IO.MemoryStream(arrImage)
  7.         PictureBox1.Image = Image.FromStream(mstream)
  8.         PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
  9.  
  10.     End Sub

Step 10

Double click the “Browse” button to open the click event handler of it and do the following code to get an image in the explore and place it in the picture box.
  1.  
  2.        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
  3.             PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
  4.         End If

Step 11

Double click again the “Update” button to open the click event handler of it and do the following codes to update the image in the database.
  1.  
  2.     sql = "UPDATE tblimage SET img = @img WHERE ID=" & DataGridView1.CurrentRow.Cells(0).Value
  3.         executeQuery(sql)
  4.  
  5.         sql = "Select ID,img as Picture From tblimage"
  6.         loadData(sql, DataGridView1)
Note : The database file is in the bin folder of the project. Download the complete source code and run it on your computer. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below

Comments

Thank you for this wonderful project, please i want you to assist me with complete vb.net tutorial in pdf, most especially in the area of access database. my contact is [email protected]

Add new comment