06/10/2011

How to add image in Ms-access database? Vb.Net code!

Adding images is database through Vb.net is very simple. Just add a data field in your database for accepting image. For example I have created a database name "Picture" and data table in it named "picroll" with following field name and data type. For better understanding of these type of vb.net code or database related inquiry the book written by Mike Murach is very good book which is available on amazon with free shipping charge, I will suggest one can  refer



       Field Name                                                  Data type
  • sno                                                             Text
  • Ename                                                        Text
  • pic                                                               OLE Object
The design a window form with following control
       Control                                 Property                    set
  • Label1                                 Text property          S.No
  • Label2                                 Text property          Name
  • Textbox1                             Name property        textbox1
  • Tetxtboxt2                          Name Property        texttbox2
  • Picturebox                          Name Property        pbNewImage
  • Button1                               Text property           Add  and Name property as BntAdd
  • Button2                               Text property           Save and Name property as BntSave
  • Button3                                Text property          Exit and Name property as BntExit
  • Imagelist





Then take a oledb data adapter name it Dapic and create a connection with your database connection data table which your created for our example it is Picroll and bind the dataset with the texttbox and picture box. and set the sizeMode property of the picture box control to StrechImage.



The write down following code 
On form Load event

Inherits System.Windows.Forms.Form
Dim bm As BindingManagerBase



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DaPic.Fill(DsPic1, "picroll")
        bm = Me.BindingContext(DsPic1, "picroll")
    End Sub


On Add Button event

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
       
        Dim eid As Integer
        If bm.Count = 0 Then
            eid = 1
        Else
            bm.Position = bm.Count - 1
            eid = CInt(TextBox2.Text) + 1
        End If
        bm.AddNew()
        TextBox2.Text = eid.ToString
        TextBox2.Enabled = False

        Dim OpenFileDialog1 As New OpenFileDialog
        With OpenFileDialog1

            .CheckFileExists = True

            .ShowReadOnly = False

            .Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"

            .FilterIndex = 2

            If .ShowDialog = DialogResult.OK Then

                ' Load the specified file into a PictureBox control.

                pbNewImage.Image = Image.FromFile(.FileName)

            End If
        End With
            End Sub

On Save Button
    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        bm.EndCurrentEdit()
        DaPic.Update(DsPic1, "picroll")
        MsgBox(" Saved")
    End Sub

On Exit Button Event
Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
        Me.Close()

    End Sub

Please give your comment and suggestion if any. Happy Coding