08/10/2011

How to retrieve photo from Ms Access database? VB.Net code

It is very easy I have tried by creating a function and calling that function at the place ,where it is needed. Just for example  prepare a windows form and create a database in Ms Access as I have described in my previous post. and follow all the step as mentioned in that post.In code editor just create a function


Step -1
on top of the code editor import following
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO


Create a function

Private Sub ShowDetails()
         Dim sEmpID As String
        sEmpID = ("Enter sno")
        If sEmpID.Trim = "" Then
            Exit Sub
        End If
        Try
            Dim cn As New OleDb.OleDbConnection
            Dim cmd As OleDb.OleDbCommand
            Dim dr As OleDb.OleDbDataReader
            cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Ram Ayodhya Singh\My Documents\Picture.mdb"
            cn.Open()
            cmd = cn.CreateCommand()
            cmd.CommandText = "select * from picroll where sno = '" & sEmpID & "'"
            dr = cmd.ExecuteReader
            If dr.Read Then
                Dim bytImage() As Byte
                Try
                    bytImage = CType(dr(2), Byte())
                     Dim ms As New System.IO.MemoryStream(bytImage)
                     Dim bmImage As New Bitmap(ms)
                     ms.Close()
                     PbNewImage.Image = bmImage
                     PbNewImage.Refresh()
                 Catch ex As Exception
                     MsgBox(ex.ToString)
                End Try
             End If
              dr.Close()
             cn.Close()
              cmd.Dispose()
             cn.Dispose()
         Catch ex As Exception

            MsgBox(ex.ToString)

        End Try
end sub
step -2 then place a button on your windows form name it "BtnShow"and text property "Show". and on the click event  call this function in addition to other information you want to show. In my code I have show the photo of an individual along with in unique identification number and name.

Private Sub BtnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnShow.Click
        If txtSNo.Text = "" Then
            MsgBox("Please enter sno")
            txtSNo.Focus()
        End If
        Try
             Dim sEmpID As String
            sEmpID = txtSNo.Text
            Dim CmdER As New OleDbCommand
            Dim MyReader As OleDb.OleDbDataReader
            Dim BitmapFromDB As Bitmap = Nothing
            CmdER.Connection = cn
            CmdER.Connection.Open()
            CmdER.CommandText = "select * from picroll where sno = '" & sEmpID & "'"
            MyReader = CmdER.ExecuteReader
            If MyReader.Read() Then
                txtSNo.Text = MyReader("sno")
                TxtName.Text = MyReader("Ename")
                ShowDetails()(Here it is show the phot of the indidvidual)
            Else
                MsgBox("This s.no is not a member of this database")
                txtSNo.Text = (" ")
                txtSNo.Focus()
            End If
             MyReader.Close()
        Catch ex As Exception
            Dim MessageString As String = "Report this error to the system
            MessageBox.Show(MessageString, TitleString, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub
To understand Vb.Net in better way I will suggest you to have  book written by Anne Prince  "Murach Visual Basic" and that is available on  Amozon and Flipkart
  Hope this will solve your problem. For More Such code refere above mentioned book

2 comments:

  1. My Dear Ram Sing,
    You wrote
    bytImage = CType(RDR1(0), Byte())
    Dim ms As New System.IO.MemoryStream(bytImage)
    Dim bmImage As New Bitmap(ms)
    ms.Close()
    PIC1.Image = bmImage
    PIC1.Refresh()

    In the line--
    "Dim bmImage As New Bitmap(ms)"
    there is an error...
    "Parameter is not valid"..
    can you tell why is this error..

    Plz send your reply to
    veedeesoft@gmail.com

    Thanks for help
    Dev..

    ReplyDelete

What & How