Search code examples
mysqlvb.netpicturebox

Store file path in MySQL database and retrieve it using vb2008


I have a form with listbox and picturebox. The lisbox is populated with filename from the database. If the user select one item from listbox the picture should appear on the picturebox. In the MySQL database, I have a table Pix with fields pixName(varchar), pLocation(varchar). In pLocation I saved the file path of the picture.

Call Connection()
    With Me
        STRSQL = "Select pLocation From Pix where pixName = " & lstPix.SelectedItem & "'"
        Try
            myCmd.Connection = myConn
            myCmd.CommandText = STRSQL
            reader = myCmd.ExecuteReader
            If reader.Read Then
                Dim path = reader.GetString(0)
                picBox.ImageLocation = path
            End If
        Catch ex As Exception
            MsgBox("no picture")
        End Try
    End With

There's no error when I run it but when I select one in listbox it says "no picture". How can I get this to work? Also, can I use the same code to play video in wmp inside the form?


Solution

  • Its clear that your query is missing apostrophe. Change The query to :

    STRSQL = "Select pLocation From Pix where pixName = '" & lstPix.SelectedItem.ToString() & "'"
    

    And a better solution is to use parameters instead :

    STRSQL = "Select pLocation From Pix where pixName = @pixName"
    myCmd.Parameters.AddWithValue("@pixName",lstPix.SelectedItem.ToString())