简体   繁体   中英

Getting error as “Parameter is not valid” while fetching image from MySQL database to PictureBox (vb.net)

I took a DataGridView and on MouseClick fetching data from database table 'products', column 'pimg'.
I took reference of this link https://www.sourcecodester.com/tutorials/visual-basic-net/12592/how-retrieve-image-mysql-database-using-vbnet.html
When I try to show fetched image in picturebox then it shows the error as "Parameter is not valid"

Private Sub DataGridViewdb_MouseClick(sender As Object, e As MouseEventArgs) Handles DataGridViewdb.MouseClick
        Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=id12302075_bdukan")
        Dim da As New MySqlDataAdapter
        Try
            Dim i As Integer
            i = DataGridViewdb.CurrentRow.Index
            Me.Labelid.Text = DataGridViewdb.Item(0, i).Value
            Me.TextBoxpid.Text = DataGridViewdb.Item(1, i).Value
            Me.TextBoxcid.Text = DataGridViewdb.Item(2, i).Value
            Me.TextBoxuid.Text = DataGridViewdb.Item(3, i).Value
            Me.TextBoxpname.Text = DataGridViewdb.Item(4, i).Value
            Me.TextBoxpyprice.Text = DataGridViewdb.Item(5, i).Value
            Me.TextBoxpprice.Text = DataGridViewdb.Item(6, i).Value
            Me.TextBoxpweight.Text = DataGridViewdb.Item(7, i).Value
            Me.TextBoxpstock.Text = DataGridViewdb.Item(8, i).Value

            connection.Open()

            Dim cmd = New MySqlCommand("select pimg from products where id='" &
              DataGridViewdb.Item(0, i).Value & "'", connection)


            Dim dt As New DataTable

            Dim arrImage() As Byte

            da.SelectCommand = cmd
            da.Fill(dt)

            arrImage = dt.Rows(0).Item(0)
            Dim mstream As New System.IO.MemoryStream(arrImage)
            PictureBox1.Image = Image.FromStream(mstream)



        Catch ex As Exception
            MessageBox.Show(ex.Message)
            da.Dispose()
            connection.Close()

        End Try

    End Sub

enter image description here

I changed the name of DataGridViewdb to DataGridView1 so it would match my test program. I left out the assignments to text boxes and such as it is not part of your problem.

Using...End Using blocks will take care of closing and disposing your database objects even if there is an error.

Always use parameters. It helps to avoid sql injection and makes your sql command easier to write (no single quotes to deal with). It also helps to kept you aware of potential type mismatches. I guessed at the datatype of your id field. Check your database and adjust the code accordingly. With you concatenated sql command you are passing a string with the single quotes. Most id fields are Integers.

Since you are only retrieving a single column in a single row, you can use .ExecuteScalar .

Private Sub DataGridView1_MouseClick(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseClick
    Dim i = DataGridView1.CurrentRow.Index
    Dim b As Object
    Using connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=id12302075_bdukan"),
            cmd As New MySqlCommand("select pimg from products where id= @id;", connection)
        cmd.Parameters.Add("@id", MySqlDbType.Int32).Value = CInt(DataGridView1.Item(0, i).Value)
        connection.Open()
        b = cmd.ExecuteScalar
    End Using
    Dim arrImage = CType(b, Byte())
    Dim mstream As New System.IO.MemoryStream(arrImage)
    PictureBox1.Image = Image.FromStream(mstream)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM