Upload Image VB.NET and MS Access Tutorial and Source code

Upload Image VB.NET and MS Access Tutorial and Source code

Problem

Create a Windows Form Application program in Visual Basic.Net that will allow users to upload an image and store in MS Access database.

Description

This tutorial is a step by step tutorial on how to upload an image in Visual basic.net and MS Access

Before the tutorial the following are required to start:

  • Microsoft Visual Studio 2008 – Above

The tutorial starts here:

  1. Open Microsoft Visual Studio 2012
  2. Select a New Project on the File menu.
  3. Select Visual Basic, Windows Form Application then click OK.
  4. We need to design our form by the following controls:
  • 4 Labels – 4 labels for the Search, Id Number, Username, and Password.
  • 4 Text boxes – text boxes for the Search, Id Number, Username, and Password Text Box.
  • 4 Command buttons – 1 button to search an ID, 1 button to browse pictures, 1 button to insert/save an information, 1 button to update an information.
  • 1 Picture Box – Picture container.
  1. We will also name our form controls in this way:
  • txtSearch is the name of the textbox for the Search text box.
  • txtID is the name of the textbox for the ID text box.
  • txtUsername is the name of the textbox for the Username text box.
  • txtPassword is the name of the textbox for the password text box.
  • cmdSearch is the name of the button for the search button.
  • cmdBrowse is the name of the button for the browse button.
  • cmdInsert is the name of the button for the insert button.
  • cmdUpdate is the name of the button for the Update Button.
  • pbImage is the name of the picture box to display an image.
  1. This is how we design the form. (Feel free to layout your own)
Upload Image VB.NET and MS Access Tutorial and Source code - Form Design
Upload Image VB.NET and MS Access Tutorial and Source code – Form Design

Figure 1. Design of the Form

  1. Open MS Acesss and add new database using MS Access database (2002 – 2003 format) with a name “testdb”, name a table “tbl_accounts” then add the following items:
  • id_number with short text data type
  • user_name with short text data type
  • pass_words with short text data type
  • addmin_Image with OLE Object data type
  • File_Size with short text data type

­­

Save the database into the project’s directory.

Upload Image VB.NET and MS Access Tutorial and Source code - Step 7
Upload Image VB.NET and MS Access Tutorial and Source code – Step 7
  1. Add a module and paste the code below

Code here

Imports System.Data.OleDb

Module Connection_Database
    Public conn As OleDbConnection
    Public cmd As OleDbCommand
    Public da As OleDbDataAdapter
    Public dt As DataTable
    Public dr As OleDbDataReader
    Public Result As Integer
    Public Sql As String
    Public Sub DatabaseConnect()
        Try
            conn = New OleDbConnection
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;Data Source=../debug/testdb.mdb"
            conn.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Module

End Code

Code Explanation:

This will serve as a connection to the MS Access database

  1. Double click the window form and paste the following code above the Public Class Form1

Code here

Imports System.Data.OleDb

End Code

Code Explanation:

The code above is important to connection of MS Access database to the system

  1. Double click the Insert Button and paste the following code.

Code here

If txtid.Text = "" Or txtpassword.Text = "" Or txtusername.Text = "" Then
            MsgBox("Supply All Needed Info!", MsgBoxStyle.Critical)
        Else
            Try
                DatabaseConnect()
                Dim FileSize As UInt32
                Dim mstream As New System.IO.MemoryStream()
                pbimage.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
                Dim arrImage() As Byte = mstream.GetBuffer()
                FileSize = mstream.Length
                mstream.Close()
                Sql = "Insert into tbl_accounts ([id_number],[user_name],[pass_words],[addmin_img],[File_size])Values(@idnumber,@username,@password,@addmin_img,@filesize)"
                cmd = New OleDbCommand
                With cmd
                    .Connection = conn
                    .CommandText = Sql
                    .Parameters.Clear()
                    .Parameters.AddWithValue("@idnumber", txtid.Text)
                    .Parameters.AddWithValue("@username", txtusername.Text)
                    .Parameters.AddWithValue("@password", txtpassword.Text)
                    .Parameters.AddWithValue("@addmin_img", arrImage)
                    .Parameters.AddWithValue("@filesize", FileSize)
                    .ExecuteNonQuery()
                    MsgBox("Record has been Add", MsgBoxStyle.Information)
                    txtid.Text = ""
                    txtpassword.Text = ""
                    txtusername.Text = ""
                    pbimage.Image = Nothing
                End With
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                conn.Close()
                cmd.Dispose()
            End Try
End If

End Code

Code Explanation:

It will save/upload all the information including the Image to the database. It will also request a connection to the database.

­­

Upload Image VB.NET and MS Access Tutorial and Source code - Step 10
Upload Image VB.NET and MS Access Tutorial and Source code – Step 10
  1. Double click the browse button and paste the following code.

Code here

   Dim ofd As New OpenFileDialog
        ofd.FileName = ""
        ofd.Filter = "PNG & JPG Supported Files Only(*.png,*jpg)|*.png;*.jpg"
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            pbimage.Image = Image.FromFile(ofd.FileName)
        End If

End Code

Code Explanation:

This will show an Open file dialogue, supporting only .png and .jpg file extension, and display the selected image to the picture.

  1. Double click the Search Button and paste the following code.

Code here

  Try
            DatabaseConnect()
            Sql = "Select * From tbl_accounts Where id_number = @idnumber"
            cmd = New OleDbCommand
            With cmd
                .Connection = conn
                .CommandText = Sql
                .Parameters.Clear()
                .Parameters.AddWithValue("@idnumber", txtsearch.Text)
            End With
            da = New OleDbDataAdapter
            dt = New DataTable
            Dim arrimg() As Byte
            Try
                da.SelectCommand = cmd
                da.Fill(dt)
                txtid.Text = dt.Rows(0).Item("id_number")
                txtusername.Text = dt.Rows(0).Item("user_name")
                txtpassword.Text = dt.Rows(0).Item("pass_words")
                arrimg = dt.Rows(0).Item("addmin_img")

                Dim mstream As New System.IO.MemoryStream(arrimg)
                pbimage.Image = Image.FromStream(mstream)
                MsgBox("Records has been Found", MsgBoxStyle.Information)
            Catch ex As Exception
            End Try
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
            da.Dispose()
        End Try

End Code

Code Explanation:

This will search the ID number from the database and display the information to the corresponding text boxes, if found.

  1. Double click the Update Button and paste the following code.

Code here

Try
            DatabaseConnect()
            Dim FileSize As UInt32
            Dim mstream As New System.IO.MemoryStream()
            pbimage.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
            Dim arrImage() As Byte = mstream.GetBuffer()
            FileSize = mstream.Length
            mstream.Close()
            Sql = "Update tbl_accounts set user_name='" & txtusername.Text & "',Pass_words='" & txtpassword.Text & "',addmin_img=@arr" & ",File_size=@File" & " where id_number='" & txtid.Text & "'"
            cmd = New OleDbCommand(Sql, conn)
            With cmd
                .Connection = conn
                .CommandText = Sql
                .Parameters.Clear()
                .Parameters.AddWithValue("@arr", arrImage)
                .Parameters.AddWithValue("@File", FileSize)
                Result = .ExecuteNonQuery()
                If Result = 0 Then
                    MsgBox("No records to Update!", MsgBoxStyle.Exclamation)
                Else
                    MsgBox("The Record has been Updated!", MsgBoxStyle.Information)
                    txtid.Text = ""
                    txtpassword.Text = ""
                    txtusername.Text = ""
                    pbimage.Image = Nothing
                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
           'cmd.Dispose()
        End Try

End Code

Code Explanation:

The code will look for the ID from the database and will update changes from the textboxes or picture box once found.

Upload Image VB.NET and MS Access Tutorial and Source code - Final Output
Upload Image VB.NET and MS Access Tutorial and Source code – Final Output

Author:

Name: Charlie Devera
Email Address: charliedevera000@gmail.com

Free Download Source code (Upload Image VB.NET and MS Access)

You may visit our facebook page for more information, inquiries and comments.

Hire our team to do the project.

­­­

, , ,

Post navigation