Database and Image display?

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
3 posts Page 1 of 1
Contributors
User avatar
hungryhounduk
VIP - Site Partner
VIP - Site Partner
Posts: 2870
Joined: Mon Jul 27, 2009 11:58 am

Database and Image display?
hungryhounduk
Hey

I have been working with databases creating a contacts application, Is there any way I can add Images to the database and display an Image everytime i choose a contact in the Database??

Or if i cant do it directly with the database is there another way I can do it with an Image List tied to the Database??

Any Help will be most gratefull and I will give out 1000 CREDITS to anyone who can create a Working example of what i am after and pm me the source, so i can see how its all structured...

Cheers

Chris
Image
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: Database and Image display?
Shim
i was able to find a working example

This is a simple code snippet which is used to store and retrieve images from Access database using VB.net.
Code: Select all
Private Sub ShowDetails()
    Try
        Dim cn As New OleDb.OleDbConnection
        Dim cmd As OleDb.OleDbCommand
        Dim dr As OleDb.OleDbDataReader

        cn.ConnectionString = mstrConnection
        cn.Open()

        cmd = cn.CreateCommand()
        cmd.CommandText = "SELECT I_Image FROM tblImage WHERE I_Name = '" & cbI_Name.Text & "'"

        dr = cmd.ExecuteReader

        If dr.Read Then
            Dim bytImage() As Byte

            Try
                bytImage = CType(dr(0), Byte())
                Dim ms As New System.IO.MemoryStream(bytImage)
                Dim bmImage As New Bitmap(ms)
                ms.Close()

                pbI_Image.Image = bmImage
                pbI_Image.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

Private Sub SaveData()
    Try
        Dim cn As New OleDb.OleDbConnection
        Dim cmd As OleDb.OleDbCommand

        cn.ConnectionString = mstrConnection
        cn.Open()

        cmd = cn.CreateCommand()

        If mstrFlag = "N" Then
            cmd.CommandText = "INSERT INTO tblImage VALUES (@I_Name, @I_Image)"
        ElseIf mstrFlag = "M" Then
            cmd.CommandText = "UPDATE tblImage SET I_Name = @I_Name, I_Image = @I_Image WHERE I_Name = '" & cbI_Name.Tag.ToString & "'"
        End If

        Dim bytImage() As Byte

        Try
            Dim ms As New System.IO.MemoryStream
            Dim bmpImage As New Bitmap(pbI_Image.Image)

            bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
            bytImage = ms.ToArray()
            ms.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        cmd.Parameters.Add(New OleDb.OleDbParameter("@I_Name", OleDb.OleDbType.VarChar, 120))
        cmd.Parameters.Add(New OleDb.OleDbParameter("@I_Image", OleDb.OleDbType.Binary))
        cmd.Parameters("@I_Name").Value = cbI_Name.Text
        cmd.Parameters("@I_Image").Value = bytImage

        If cmd.ExecuteNonQuery() > 0 Then
            MsgBox("Record has been " & IIf(mstrFlag = "N", "added", "modified").ToString & " successfully.", MsgBoxStyle.Information)
        End If

        cmd.Dispose()
        cn.Dispose()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

    FillData()
End Sub
source code
ImageDb_src_vb.zip
You do not have the required permissions to view the files attached to this post.
Find my programs on Softpedia
User avatar
hungryhounduk
VIP - Site Partner
VIP - Site Partner
Posts: 2870
Joined: Mon Jul 27, 2009 11:58 am

Wow!!!!!

Thanks a lot mshimranpro loove; That is better than i expected it too be, i like the add and save functions.

BRILLIANT

A 1000 CREDITS are being transferd cooll;

You are a Star

respect

Chris
Image
3 posts Page 1 of 1
Return to “Coding Help & Support”