hello, im going to create POS project, can you help me how to retrieve all image from mysql db to listview, so that i can view all image that i save to my db, i already save image and use sreach button on retrieve image from db to picturebox. thank you guys .

the code below is not retrieving image from db to listview, but if you click listview it will retrieve image from db to picturebox

 Dim constr As String = "Data Source = localhost; User ID = root; Database = chamchamber"
    Dim cn As MySqlConnection
    Dim cmd As MySqlCommand

    Dim da As MySqlDataAdapter
    Dim dt As DataTable
    Dim ds As DataSet
    Dim dr As MySqlDataReader

 Private Sub ListView1_Click(sender As Object, e As EventArgs) Handles ListView1.Click

        cn = New MySqlConnection(constr)
        cn.Open()
        cmd = New MySqlCommand
        cmd.Connection = cn

        cmd.CommandText = "SELECT * from category where id= '" & ListView1.SelectedItems(0).Text & "'"
        dr = cmd.ExecuteReader()
        If (dr.HasRows) Then
            While (dr.Read())
                With Me
                    'fetch image from database
                    Dim imgBytes() As Byte = dr("IMG") 'image field
                    Dim image As Bitmap = New Bitmap(New System.IO.MemoryStream(imgBytes)) 'convert binary to image

                    PictureBox1.Image = image 'show picture to picture box
                End With
            End While
        Else
            MsgBox("No records Found!")
        End If
    End Sub

Recommended Answers

All 8 Replies

but now it works

Imports MySql.Data.MySqlClient
Imports System.IO

Public Class Form1

Dim constr As String = "Data Source = localhost; User ID = root; Database = chamchamber"
Dim cn As MySqlConnection
Dim cmd As MySqlCommand
Dim da As MySqlDataAdapter
Dim dr As MySqlDataReader

Dim dt As DataTable
Dim ds As DataSet

Dim itemcoll(100) As String

Private Sub categorylist() ' reitrieve data from mysql to listview

    Try
        ListView1.Items.Clear()
        cn = New MySqlConnection(constr)
        cn.Open()
        cmd = New MySqlCommand
        cmd.Connection = cn
        cmd.CommandText = "Select * from category"
        da = New MySqlDataAdapter(cmd)
        ds = New DataSet

        da.Fill(ds, "category")
        For r = 0 To ds.Tables(0).Rows.Count - 1
            For c = 0 To ds.Tables(0).Columns.Count - 1
                itemcoll(c) = ds.Tables(0).Rows(r)(c).ToString
            Next
            Dim lvitem As New ListViewItem(itemcoll)
            ListView1.Items.Add(lvitem)
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    cn.Close()

End Sub

Private Sub retrieve()'retrieve image from mysql
    Try
        ListView2.Clear()
        Dim imglist As New ImageList
        imglist.ColorDepth = ColorDepth.Depth32Bit
        ListView2.LargeImageList = imglist
        ListView2.LargeImageList.ImageSize = New System.Drawing.Size(100, 100)
        constr = "select * from category"

        Dim dt_images As New DataTable

        cmd.Connection = cn
        cmd.CommandText = constr
        da.SelectCommand = cmd

        da.Fill(dt_images)
        For Each dr As DataRow In dt_images.Rows
            Dim img_buffer = CType(dr("IMG"), Byte())
            Dim img_stream As New MemoryStream(img_buffer, True)

            img_stream.Write(img_buffer, 0, img_buffer.Length)
            imglist.Images.Add(dr("ID").ToString(), New Bitmap(img_stream))
            img_stream.Close()

            Dim lsvparent As New ListViewItem

            lsvparent.Text = dr("IMG").ToString
            lsvparent.ImageKey = dr("ID").ToString
            ListView2.Items.Add(lsvparent)
        Next
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    categorylist() ' call faction from top
    retrieve()
End Sub

End Class

ssssss.png

but now its work now my problem, i dont know how to remove the system byte under the image

i dont know how to remove the system byte and change it to "NAME" of the item

Is the name being generated by "dr("ID").ToString()"?

Private Sub retrieve()

    ListView2.Items.Clear()
    cn = New MySqlConnection(constr)
    cn.Open()
    cmd = New MySqlCommand
    cmd.Connection = cn
    cmd.CommandText = "Select * from category"
    da = New MySqlDataAdapter(cmd)
    ds = New DataSet

    Try

        ListView2.Clear()
        Dim imglist As New ImageList
        imglist.ColorDepth = ColorDepth.Depth32Bit
        ListView2.LargeImageList = imglist
        ListView2.LargeImageList.ImageSize = New System.Drawing.Size(100, 100)
        constr = "select * from items"

        Dim dt_images As New DataTable

        cmd.Connection = cn
        cmd.CommandText = constr
        da.SelectCommand = cmd

        da.Fill(dt_images)
        For Each dr As DataRow In dt_images.Rows
            Dim img_buffer = CType(dr("IMAGE"), Byte())

            Dim img_stream As New MemoryStream(img_buffer, True)

            img_stream.Write(img_buffer, 0, img_buffer.Length)
            imglist.Images.Add(dr("ID").ToString(), New Bitmap(img_stream))
            img_stream.Close()

            Dim lsvparent As New ListViewItem

            lsvparent.Text = dr("IMAGE").ToString     'if you remove it the system byte will remove to or hide
            lsvparent.ImageKey = dr("ID").ToString       ' if you remoce it the image will hide

            ListView2.Items.Add(lsvparent)
        Next
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
End Sub

the name is not generated by the ID

im the one who set the name

Private Sub retrieve()

    ListView2.Items.Clear()
    cn = New MySqlConnection(constr)
    cn.Open()
    cmd = New MySqlCommand
    cmd.Connection = cn
    ' cmd.CommandText = "Select * from category"
    da = New MySqlDataAdapter(cmd)

    Try

        ListView2.Clear()
        Dim imglist As New ImageList
        imglist.ColorDepth = ColorDepth.Depth32Bit
        ListView2.LargeImageList = imglist
        ListView2.LargeImageList.ImageSize = New System.Drawing.Size(100, 100)
        constr = "select * from items"

        Dim dt_images As New DataTable

        '   cmd.Connection = cn
        cmd.CommandText = constr
        da.SelectCommand = cmd

        da.Fill(dt_images)
        For Each dr As DataRow In dt_images.Rows
            Dim img_buffer = CType(dr("IMAGE"), Byte())

            Dim img_stream As New MemoryStream(img_buffer, True)

            img_stream.Write(img_buffer, 0, img_buffer.Length)
            imglist.Images.Add(dr("ID").ToString(), New Bitmap(img_stream))
            img_stream.Close()

            Dim lsvparent As New ListViewItem

            lsvparent.Text = dr("IMAGE").ToString     'if you remove it the system byte will remove to or hide
            lsvparent.ImageKey = dr("ID").ToString       ' if you remoce it the image will hide

            ListView2.Items.Add(lsvparent)
        Next
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.