Wednesday 5 October 2016

Save Image In Access and Retrieve in Datagridview Using VB.net

This small project will help you to understand how can you handle image processing in your VB.net Window Application. There are many times when we want to save images in our project software for instance  in school software Saving student image. So you can achieved this through following approach

  1. Saving Image into database
  2. Saving image into a folder
First approach is not so much popular because image get more size than normal data  So your database saved thousands of byte for an image which lead decline in record retrieval. if you want to follow this approach click on Following Link

Save Image into MS Access  database

This post deal with second approach because its good practice to save information into database and image in a folder . you can save image name into database or rename uploaded image as your table  ID (PK) field value. I am saving image name into database which is easy to implement but renaming image is more suitable because its decrease  database storage.

Screen Shot

Add Image
Image Add Screen



Show Image
Show Image

Image Add Form Code :


Imports System.Data.OleDb
Public Class frmAdd

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If txtName.Text = "" Or txtcity.Text = "" Or OpenFileDialog1.FileName = "OpenFileDialog1" Then
            MsgBox("Please  provide all value")
            If txtcity.Text = "" Then
                txtcity.Focus()
            ElseIf txtName.Text = "" Then
                txtName.Focus()
            ElseIf OpenFileDialog1.FileName = "OpenFileDialog1" Then
                Button1.Focus()
            End If
        Else
            Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=girfa.accdb")
            Dim com As New OleDbCommand
            Try
                com.Connection = con
                con.Open()
                com.CommandText = "insert into stu (sname,city,pic) values('" & txtName.Text & "','" & txtcity.Text & "','" & System.IO.Path.GetFileName(OpenFileDialog1.FileName) & "')"
                com.ExecuteNonQuery()
                FileCopy(OpenFileDialog1.FileName, Application.StartupPath & "\image\" & System.IO.Path.GetFileName(OpenFileDialog1.FileName))
                MsgBox("Record Saved")
                txtName.Text = ""
                txtName.Focus()
                txtcity.Text = ""
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                con.Close()
            End Try
        End If       
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
    End Sub
    
End Class

Show Image Form Code : 

Imports System.Data.OleDb
Public Class frmShow

    Private Sub frmShow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '
        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=girfa.accdb")
        Dim com As New OleDbCommand("select * from stu", con)
        Try
            com.CommandType = CommandType.Text
            Dim da As New OleDbDataAdapter(com)
            Dim ds As New DataSet()
            da.Fill(ds)
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

    End Sub

    Private Sub DataGridView1_RowHeaderMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)Handles DataGridView1.RowHeaderMouseClick
        Dim dr As DataGridViewSelectedRowCollection
        dr = DataGridView1.SelectedRows()
        'lblid.Text = dr.Item(0).Cells(1).Value.ToString()
        lblroll.Text = dr.Item(0).Cells(0).Value.ToString
        lblName.Text = dr.Item(0).Cells(1).Value.ToString
        lblCity.Text = dr.Item(0).Cells(2).Value.ToString
        PictureBox1.Image = Image.FromFile(Application.StartupPath & "/image/" & dr.Item(0).Cells(3).Value.ToString)

    End Sub
End Class

Download Full Source Code

Next Example

No comments:

Post a Comment