Monday 4 June 2012

Save and Retrieve an image in Ms Access Using VB.Net

Hi.......
Its very simple to save and retrieve image in ms access for performing save retrieve you need to do something as mention...........
  • Aceess store image in binary format
  • We've to convert an image into binary at save time and binary ti bitmap at retrieve time

Database Table Structure 

 

 

 

 

 

 

 

 

 

  


 

 

 

 

Project form structure 

 

 

 

 

 

 




  • You've to use OpenFileDialog for select image which is not appear in this picture
  •  Make a string type global variable named str
  • Set Data grid properties for showing table data you need to only set some property and data grid view show your table data

Save Button Code

 Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Raj\vbNet\ReadWriteInAccess\bsw.mdb")
        Dim com As New OleDbCommand()
        Try
            Dim fs As New FileStream(Str, FileMode.Open)
            Dim data() As Byte = New [Byte](fs.Length) {}
            fs.Read(data, 0, fs.Length)
            fs.Close()
            'readed image
            Dim roll As New OleDbParameter("roll", OleDbType.Integer)
            roll.Value = Val(txtRoll.Text)

            Dim name As New OleDbParameter("name", OleDbType.VarChar, 50)
            name.Value = txtName.Text

            Dim city As New OleDbParameter("city", OleDbType.VarChar, 50)
            city.Value = txtCity.Text

            Dim img As New OleDbParameter("img", OleDbType.Binary)
            img.Value = data

            'Adding parameters
            com.Parameters.Add(roll)
            com.Parameters.Add(name)
            com.Parameters.Add(city)
            com.Parameters.Add(img)
            con.Open()
            com.Connection = con
            com.CommandText = "insert into stu values(@roll,@name,@city,@img)"
            com.ExecuteNonQuery()
            MsgBox("Saved")
            Me.StuTableAdapter.Fill(Me.BswDataSet1.stu)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        OpenFileDialog1.ShowDialog()
        str = OpenFileDialog1.FileName
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If txtRoll.Text = "" Then
            MsgBox("plz enter roll no")
            txtRoll.Focus()
        Else
            Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Raj\vbNet\ReadWriteInAccess\bsw.mdb")
            Dim com As New OleDbCommand()
            Try
                PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
                com.Connection = con
                con.Open()
                com.CommandText = "select * from stu where roll=" & txtRoll.Text
                Dim dr As OleDbDataReader = com.ExecuteReader()
                dr.Read()
                If dr.HasRows Then
                    Dim bits As Byte() = CType(dr("img"), Byte())
                    Dim memo As New MemoryStream(bits)
                    Dim myimg As New Bitmap(memo)
                    PictureBox1.Image = myimg

                Else
                    MsgBox("Record not found")
                End If

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If



Show Button Code 

If txtRoll.Text = "" Then
            MsgBox("plz enter roll no")
            txtRoll.Focus()
        Else
            Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Raj\vbNet\ReadWriteInAccess\bsw.mdb")
            Dim com As New OleDbCommand()
            Try
                PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
                com.Connection = con
                con.Open()
                com.CommandText = "select * from stu where roll=" & txtRoll.Text
                Dim dr As OleDbDataReader = com.ExecuteReader()
                dr.Read()
                If dr.HasRows Then
                    Dim bits As Byte() = CType(dr("img"), Byte())
                    Dim memo As New MemoryStream(bits)
                    Dim myimg As New Bitmap(memo)
                    PictureBox1.Image = myimg

                Else
                    MsgBox("Record not found")
                End If

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If


I hope this post will help you to handle image........................
Raj kumar...

8 comments:

  1. Wow! Raju You have done an amazing job..!!It will help lot of people!:)) Cheers!

    ReplyDelete
  2. Hellow Sir!Conglats for this help i real do appreciate you.However! I do a problem,I'm developing membership registration system for our church,i have image box and three buttons(browse image,start camera and save button)when i save image which i browsed and pressing at image box then it saves well in ms access but when i have captured image via webcam and pressing it at image box and trying to save it i fail,please any help
    With thanks
    Richard Masua

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. it was show error parameter is not valid

    ReplyDelete
  5. in line : Dim myimg As New Bitmap(memo)
    it was show error parameter is not valid
    please help me sir, how to fix it. Thanks

    ReplyDelete