hi to all, can you help me in my program, i can't retrieve my image in the database to vb.net
how can i view my image when i click in listview? plss help me

THIS IS MY CODE HOPE YOU CAN HELP ME!

Imports MySql.Data.MySqlClient


Public Class Often_Motorcycle_Problems
    Public ConnString As String = "server=localhost;user id=root;password=;database=motorcycle"
    Public CONNECTION As New MySqlConnection(ConnString)
    Public ds As New DataSet
    Public strSQL As String
    Public cmd As New MySqlCommand
    Public dr As MySqlDataReader
    Dim a, b, c, d, e, f, g

    Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick

        If ListView1.Items.Count > 0 Then
            Motorcycle_Information.lblno.Text = ListView1.SelectedItems(0).Text
            Motorcycle_Information.lblskinname.Text = ListView1.SelectedItems(0).SubItems(1).Text
            Motorcycle_Information.lbllevel.Text = ListView1.SelectedItems(0).SubItems(2).Text
            strSQL = "SELECT * from motorproblem where id= '" & ListView1.SelectedItems(0).Text & "'"
            CONNECTION.Open()
            cmd = New MySqlCommand(strSQL, CONNECTION)
            dr = cmd.ExecuteReader()

            ListView1.Items.Clear()

            Do While dr.Read()

                d = (dr.Item("description").ToString())
                g = (dr.Item("cause").ToString())
                Motorcycle_Information.lbldesc.Text = d
                Motorcycle_Information.lblcause.Text = g
            Loop
            dr.Close()
            CONNECTION.Close()
            cmd.Dispose()

        End If
        Motorcycle_Information.Show()
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub Often_Motorcycle_Problems_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txtsearch.TextChanged
        strSQL = "SELECT * from motorproblem where partsname like '%" & Txtsearch.Text & "%'"
        CONNECTION.Open()
        cmd = New MySqlCommand(strSQL, CONNECTION)
        dr = cmd.ExecuteReader()

        ListView1.Items.Clear()

        Do While dr.Read()

            a = (dr.Item("id").ToString())
            b = (dr.Item("partsname").ToString())
            c = (dr.Item("level").ToString())

            Dim lv As ListViewItem = ListView1.Items.Add(a)
            lv.SubItems.Add(b)
            lv.SubItems.Add(c)
        Loop
        dr.Close()
        CONNECTION.Close()
        cmd.Dispose()
    End Sub

Recommended Answers

All 12 Replies

please help me.. thank a lot!

i have done one project that work with multiple picture, 3 differences from your project is :

  1. i use datagridview (not listview);
  2. i use 2 form (first form for DGV, and second form to hold picture preview when user double click the DGV);
  3. i save the path of picture in database not the picture it self (i think it more light for my database).

so, if you really want it, i will show you my work.

Sample1
Sample2

really? thank you..

ow dis is my code for addng images in mysql database..

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim FileSize As UInt32
        CONNECTION.Close()
        Dim mstream As New System.IO.MemoryStream()
        PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
        Dim arrImage() As Byte = mstream.GetBuffer
        FileSize = mstream.Length
        Dim sqlcmd As New MySqlCommand
        Dim sql As String
        mstream.Close()

        sql = "insert into motorproblem(partsname,description,cause,level, images) values ('" & txtpartsname.Text & "', '" & txtdescription.Text & "', '" & txtcause.Text & "', '" & txtlevel.Text & "', @File)"
        Dim da As New MySqlDataAdapter(strSQL, CONNECTION)
        Try
            CONNECTION.Open()
            With sqlcmd
                .CommandText = sql
                .Connection = CONNECTION
                .Parameters.AddWithValue("@File", arrImage)

                .ExecuteNonQuery()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            CONNECTION.Close()
        End Try


        txtpartsname.Text = ""
        txtdescription.Text = ""
        txtcause.Text = ""
        txtlevel.Text = ""
        txtpartsname.Focus()


    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim openfile As New OpenFileDialog
        If openfile.ShowDialog = Windows.Forms.DialogResult.OK Then

        End If
        PictureBox1.Image = Image.FromFile(openfile.FileName)

    End Sub

    Private Sub ListView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick


        If ListView1.Items.Count > 0 Then
            MotorEdit.lblid.Text = ListView1.SelectedItems(0).Text
            MotorEdit.txtpartsname.Text = ListView1.SelectedItems(0).SubItems(1).Text
            MotorEdit.cbolevel.Text = ListView1.SelectedItems(0).SubItems(2).Text
            MotorEdit.txtdescription.Text = ListView1.SelectedItems(0).SubItems(3).Text
            MotorEdit.txtcause.Text = ListView1.SelectedItems(0).SubItems(4).Text
        End If
        MotorEdit.Show()
    End Sub

can u help me for retrieving? i have listview for viewing the content of my records..
then if you double click the record another form will appear and there is the record..
the problem is i can't display the image from my database there's always an error.. please help me.

i assume that button3 to retrieve image, since i don't know how you write your connection string so maybe you'll need some modification with this code :

 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
 try
 using conn as new mysqlconnection("server=blablabla;user=blablabla;password=blablabla;database=blablabla")
 conn.open()
Dim command As New MySqlCommand("SELECT * FROM table_name WHERE column_name = blablabla", conn)
            Dim data As MySqlDataReader = command.ExecuteReader()
            If data.HasRows = True Then
                data.Read()
                Dim bt As Byte() = DirectCast(data("images"), Byte())
                Dim ms As New MemoryStream(bt)
                PictureBox1.Image = Image.FromStream(ms)
            End If
            command.Dispose()
            data.Close()
            conn.Close()
            End Using
            Catch ex As Exception
        MessageBox.Show(ex.Message)
            End Try
End Sub

sir that button 3 is to select image from my computer..
is that code can display my record and image in the form?
thank you sir for helping me..godbless

ahh.. since i don't know what button to trigger it, so put that code to your button for displaying. maybe you'll need a little bit modification because i don't know your database structure and how you write your connection string.

this is my connection sir..

Imports MySql.Data.MySqlClient

Public Class AddEditDelete
    Public ConnString As String = "server=localhost;user id=root;password=;database=motorcycle"
    Public CONNECTION As New MySqlConnection(ConnString)
    Public ds As New DataSet
    Public strSQL As String
    Public cmd As New MySqlCommand
    Public dr As MySqlDataReader

`

                Dim reader As MySqlDataReader

                Dim command As MySqlCommand = connection.CreateCommand()
                command.CommandText = "SELECT * from motorproblem where id= '" & ListView1.SelectedItems(0).Text & "'"

                reader = command.ExecuteReader()
                If (reader.HasRows) Then
                    While (reader.Read())
                        With Me


                            'fetch image from database
                            Dim imgBytes() As Byte = reader("image") 'image field
                            Dim image As Bitmap = New Bitmap(New  System.IO.MemoryStream(imgBytes)) 'convert binary to image
                            .ProfilePic.Image = image 'show picture to picture box

                        End With


                    End While

                Else

                  MSgbox "No records Found!"

                End If

`

`

   Dim reader As MySqlDataReader

                    Dim command As MySqlCommand = connection.CreateCommand()
                    command.CommandText = "SELECT * from motorproblem where id= '" & ListView1.SelectedItems(0).Text & "'"

                    reader = command.ExecuteReader()
                    If (reader.HasRows) Then
                        While (reader.Read())
                            With Me


                                'fetch image from database
                                Dim imgBytes() As Byte = reader("image") 'image field
                                Dim image As Bitmap = New Bitmap(New  System.IO.MemoryStream(imgBytes)) 'convert binary to image
                                .ProfilePic.Image = image 'show picture to picture box

                            End With


                        End While

                    Else

                      MSgbox "No records Found!"

                    End If

thank you for the code..
:)

sir...how to add and retrieve video in mysql to vb.net?

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.