user3843586
user3843586

Reputation:

Why following update query always update null value?

I am trying to update a blob field based on id, with the following code but it always insert Null.

 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        openconnection()
        Dim cmd As New Odbc.OdbcCommand("UPDATE blob_table SET image=@PictureBox1 WHERE id='6'", myconnection)
        Dim fs As New System.IO.FileStream("E:\Untitled.png", IO.FileMode.Open, IO.FileAccess.Read)
        Dim b(fs.Length() - 1) As Byte
        fs.Read(b, 0, b.Length)
        fs.Close()
        Dim P As Odbc.OdbcParameter = New Odbc.OdbcParameter("@PictureBox1", Odbc.OdbcType.Image, b.Length, ParameterDirection.Input, True, 0, 0, Nothing, DataRowVersion.Current, b)
        cmd.Parameters.Add(P)
        openconnection()
        cmd.ExecuteNonQuery()
        closeconnection()
    End Sub

I check the connection, it is properly working, The image path is a valid path. anyone can help me to find the mistake in the query?

Upvotes: 1

Views: 61

Answers (3)

alital
alital

Reputation: 431

Try using a BinaryReader to populate your b byte array: something like:

Dim b As Byte()    
Dim br As New BinaryReader(fs)    
b = br.ReadBytes(CInt(fs.Length))    
br.Close()    
fs.Close()    

Upvotes: 1

Vivek S.
Vivek S.

Reputation: 21905

you have to do something like this to insert image to DB

Using pgFileStream As FileStream = New FileStream(productImageFilePath, FileMode.Open, FileAccess.Read)
                Using pgReader As BinaryReader = New BinaryReader(New BufferedStream(pgFileStream))
                    Dim pgByteA As Byte() = pgReader.ReadBytes(CInt(pgFileStream.Length))
                    command.CommandText = " update gtab82 set memphoto=@Image where memuuid ='" & txtmemuid.Text & "' "
                    command.Parameters.AddWithValue("@Image", pgByteA)
                    command.ExecuteNonQuery()
                End Using
End Using
  • am using PostgreSQL databse and my image field is bytea

Upvotes: 0

Robert Anderson
Robert Anderson

Reputation: 1256

I think you need to set the value of @PictureBox1 to a byte array - e.g. byte[] as I understand that's what the image blob type maps to instead of byte.

Upvotes: 1

Related Questions