Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone out there, I would be grateful if someone could help me solve this problem. I want to retrieve image from the database in SQL server 2008 to vb 2012. For now am able to save the image into the database alright. But my challenge is how to retrieve it from the server. Below is the code to retrieve the image from the server.

The first code is for binding the image to the datagrid.

VB
Dim ms As New MemoryStream()
Dim imageData As Byte() = ms.GetBuffer()

Private Sub PassportPictureDataGrid_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles PassportPictureDataGrid.CellMouseClick
       
 Com = New SqlCommand
        Try
            Com = New SqlCommand("select PassportPicture from IndividualAccountPassportPictures where AccountNumber='" & _
             PassportPictureDataGrid.CurrentRow.Cells(0).Value() & "'", One99)
            Dim imageData As Byte() = DirectCast(Com.ExecuteScalar(), Byte())
            If Not imageData Is Nothing Then
                Using ms As New MemoryStream(imageData, 0, imageData.Length)
                    ms.Write(imageData, 0, imageData.Length)
                    SamplePictureBox.BackgroundImage = Image.FromStream(ms, True)
                End Using
            End If
            Label1.Visible = True
            Label1.Text = PassportPictureDataGrid.CurrentRow.Cells(0).Value()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub 


This code is for binding the image to a picture box

VB
    Private Sub btnRetrievePicture_Click(sender As Object, e As EventArgs) Handles btnRetrievePicture.Click
      
        Try
            Com = New SqlCommand("Select Photo From SampleImageTest Where PhotoName = '" & Me.cmbPictureName.Text.Trim & "'", One99)
            Dim imageData As Byte() = DirectCast(Com.ExecuteScalar(), Byte())
            If Not imageData Is Nothing Then
                Using ms As New MemoryStream(imageData, 0, imageData.Length)
                    ms.Write(imageData, 0, imageData.Length)
                    ShowPicture.BackgroundImage = Image.FromStream(ms, True)
                End Using

                 Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
        ms.Close()
        Reader33.Close()
    End Sub
End Class


Please i get the same error message for both codes and this is the error message i get. Parameter is not valid.

I hope my explanation is clear.

Thanks.
Posted
Updated 22-Sep-15 14:40pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900