i have a database at mysql and i already save image and use search button to retrieve image from mysql database to picture box, vb.net , i dont have a code how listview retrieve all image from mysql but the code is different funtion but the concet is the same.

346 Views
https://www.daniweb.com/community/contribute#
https://www.daniweb.com/community/contribute#

'this code is retrieve all the image from file locatioin

 Private Sub open_image_from_file_location_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ListView1.Items.Clear()
        'retrieve image from specific folder
        Dim dirFiles() As String = IO.Directory.GetFiles("D:\Perity Files\Komasan")
        Dim _imgList As New ImageList
        Dim imgSize As New Size
        'setting up custom size
        imgSize.Width = 100
        imgSize.Height = 50
        ListView1.LargeImageList = _imgList
        ListView1.LargeImageList.ColorDepth = ColorDepth.Depth32Bit
        Dim count As Integer = 0
        Dim item As New ListViewItem
        For Each dirFile As String In dirFiles
            Dim imgFilename As String = IO.Path.GetFileNameWithoutExtension(dirFile)
            Dim img As New System.Drawing.Bitmap(dirFile)
            Dim imgImage As Image = Image.FromFile(dirFile)
            _imgList.ImageSize = imgSize
            _imgList.Images.Add(img.Clone)
            ListView1.Items.Add(imgFilename, count)
            count += 1
        Next
    End Sub
	
	
	'this one is retrieve image from database using datagridviewer
	
	 Private Sub load_data()
        Try
            cn = New MySqlConnection
            cn.ConnectionString = "data source=localhost; user id=root; database=account;"
            cn.Open()
            constr = "SELECT ID,NAME,EMAIL,IMG FROM users;"
            cmd = New MySqlCommand(constr, cn)
            da = New MySqlDataAdapter
            dt = New DataTable
            da.SelectCommand = cmd
            da.Fill(dt)
            cn.Close()
            DataGridView1.DataSource = dt
        Catch ex As MySqlException
            MsgBox(ex.Message)
        Finally
            cn.Close()
            da.Dispose()
        End Try
    End Sub
	
	but i looking for how listview retrieve image from mysql database

i see this video but when i try the code, it show messege error
https://www.youtube.com/watch?v=HhG7ughF8Zg

Imports MySql.Data.MySqlClient
Imports System.Data
Imports System.IO

Imports System.Drawing
Imports System.Drawing.Imaging

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 drv As MySqlDataReader
Dim ds As DataSet

Private Sub retrieve()
    Try
        ListView1.Clear()
        Dim imglist As New ImageList

        imglist.ColorDepth = ColorDepth.Depth32Bit
        ListView1.LargeImageList = imglist
        ListView1.LargeImageList.ImageSize = New System.Drawing.Size(200, 200)

        Using dt_images As New DataTable

            cmd.Connection = cn
            cmd.CommandText = "select * from category"
            da = New MySqlDataAdapter(cmd)
            ds = New DataSet

            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
                ListView1.Items.Add(lsvparent)
            Next
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    retrieve()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim opf As New OpenFileDialog

    opf.Filter = "choose image(*.JPG;*.PNG;.*GIF;)|*.jpg;*.png;*.gif"
    If opf.ShowDialog = Windows.Forms.DialogResult.OK Then
        PictureBox1.Image = Image.FromFile(opf.FileName)
    End If
End Sub

End Class

What error message, and on what line?

lately i think thats an virus or code distroyer but when i restart my computer OS and install again VB and install update mysqlconnector its work
now my problem is how to remove the system byte under this picture

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()
    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

can you help me how to fix this,now i already retrieve image from the db msql, but
i dont know how to show the name of the image, but there is a systembyte under the image

  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