1

I am trying to save image in database.

Codes run successfully.

But when I retrieve the image it gives error.

Parameter is not valid on this Line:

pbDatbase.Image = Image.FromStream(msImage)

enter code here

I check in detail the problem is when I save picture I count the bytes.

It gives something like 251625 bytes.

Wen I retrieve image, it gives only sometimes 8 , sometimes 16.

Input image in datbase:

Dim strQuery As String
        strQuery = "Insert Into tblPictures(Picture) Values('@Pic')"
        Using msImage As New MemoryStream
            pbFile.Image.Save(msImage, pbFile.Image.RawFormat)
            bytImage = msImage.GetBuffer()
            MsgBox(bytImage.Count)

            Dim cmdImage As New OdbcCommand
            dbConnection.Open()
            cmdImage.Connection = dbConnection
            cmdImage.CommandText = strQuery
            cmdImage.Parameters.AddWithValue("@Pic", bytImage)
            cmdImage.ExecuteNonQuery()
            dbConnection.Close()
            cmdImage.Cancel()
        End Using

Retrieving Image From Datbase:

 Dim strQuery As String
        strQuery = "Select Picture From tblPictures Where PictureId=(Select Max(PictureId) As ProdId from tblPictures)"
        Dim cmdImage As New OdbcCommand
        dbConnection.Open()
        cmdImage.Connection = dbConnection
        cmdImage.CommandText = strQuery
        Dim drImage As OdbcDataReader
        drImage = cmdImage.ExecuteReader
        If drImage.HasRows Then
            Dim bytImage() As Byte
            bytImage = DirectCast(drImage("Picture"), Byte())
            MsgBox(bytImage.Count)
            Dim msImage As New MemoryStream(bytImage)
            pbDatbase.Image = Image.FromStream(msImage)
        End If
        dbConnection.Close()
        cmdImage.Cancel()

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.