I am new in vb.net. The image has been stored in sql server as <binary data> and i dont know how to convert it to display in picture box.Please tell me what are the changes should i made to get it to work.Here is the code which i have done so far

Imports System.Data.SqlClient
    Imports System.Data
    Public Class Edit_Employee
        Dim str As String = "Data Source=NET3\SQLEXPRESS;Initial Catalog=restpos;Persist Security Info=True;User ID=sa;Password=password"
        Dim ds As New DataSet
        Dim adp As New SqlDataAdapter
        Dim con As New SqlClient.SqlConnection
    Private Sub Edit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         adp = New SqlDataAdapter("select Emp_Image from Employees where Emp_UserId=106",con)
         con.ConnectionString = str  
         adp.Fill(ds)      
         PictureBox1.Image = ds.Tables(0).Rows(0)
      End sub   
    End Class

Recommended Answers

All 17 Replies

What is the data type in server where u save the image file?

What is the data type in server where u save the image file?

image

u need to convert that to memory stream before showing into picturebox.

I hope u know how to get ur image from DB to dataset. After filling dataset with ur image from DB write the below code.

' assuming ur getting ur image in dataset called dsImage  
           Dim saveImage As IO.MemoryStream
            Dim imgByte As Byte()
            If dsImage.Tables.Count > 0 Then
                imgByte = dsImage.Tables(0).Rows(0)("Image")
                saveImage = New IO.MemoryStream(imgByte)
                Picturebox1.Image = Image.FromStream(saveImage)
            End If

Thanks a lot for your concern Pgmer.
An error occured when i run the application. Error:Unable to cast object of type 'System.DBNull' to type 'System.Byte[]. please help

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        con.ConnectionString = str
        adp = New SqlDataAdapter("select Emp_Image from Employees Where Emp_UserId=117", con)
        adp.Fill(ds)
        Dim saveImage As IO.MemoryStream
        Dim imgByte As Byte()
        If ds.Tables.Count > 0 Then
            imgByte = ds.Tables(0).Rows(0)("Emp_Image")----- > the error has shown here
            saveImage = New IO.MemoryStream(imgByte)
            PictureBox2.Image = Image.FromStream(saveImage)
        End If
    End Sub

I just took row(0) as an example... do u have image at row(0)? and column name is proper?

Dim intImage As Integer = 0
        If ds.Tables.Count > 0 Then
            For intImage to ds.tabble(0).rows.count-1
                imgByte = ds.Tables(0).Rows(intImage)("Emp_Image")
                saveImage = New IO.MemoryStream(imgByte)
                PictureBox2.Image = Image.FromStream(saveImage)
            Next
            
        End If

thanks Pgmer.
I have entered only one row of data in database, Image is also there in row(0) and i dont know why it is showing error. An error(Syntax error) had shown when i used the code below.please help

Dim intImage As Integer = 0
If ds.Tables.Count > 0 Then
    For intImage [U]to[/U]-->(Syntax Error) ds.table(0).rows.count-1
        imgByte = ds.Tables(0).Rows(intImage)("Emp_Image")
        saveImage = New IO.MemoryStream(imgByte)
        PictureBox2.Image = Image.FromStream(saveImage)
    Next

End If
Dim intImage As Integer = 0
        If ds.Tables.Count > 0 Then
            For intImage = 0 To ds.table(0).rows.count - 1
                imgByte = ds.Tables(0).Rows(intImage)("Emp_Image")
                saveImage = New IO.MemoryStream(imgByte)
                PictureBox2.Image = Image.FromStream(saveImage)
            Next

        End If

And also check isdbnull value for ur image data.

Syntax error is fixed now.But an error(Parameter is not valid) occured when i execute this code

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        con.ConnectionString = str
        adp = New SqlDataAdapter("select Emp_Image from Employees where Emp_UserId='1' ", con)
        adp.Fill(dsimage)
        Dim saveImage As IO.MemoryStream
        Dim intImage As Integer = 0
        Dim imgByte As Byte()
        If dsimage.Tables.Count > 0 Then
            For intImage = 0 To Convert.ToInt32(dsimage.Tables(0).Rows.Count) - 1
                imgByte = dsimage.Tables(0).Rows(intImage)("Emp_Image")
                saveImage = New IO.MemoryStream(imgByte)
                PictureBox2.Image = Image.FromStream(saveImage)-->Error(Parameter is not valid)
            Next
            
        End If

This piece of code
saveImage = New IO.MemoryStream(imgByte)
pb.Image = Image.FromStream(saveImage)
works fine for me... you only can debug ur code to fix the error

PictureBox2.Image = Image.FromStream(saveImage)--> Error: Parameter is not valid

The above error has shown when i run code.What should i do to clear error?Pls tell me a solution

Did u declare the below variables?
Dim saveImage As IO.MemoryStream
Dim imgByte As Byte()

Hi,Thanks Pgmer.
I have used another method, problem solved.

commented: Hello ,anyone to help me. +0

hi ...which method did u use....cos am having the same (parameter is not valid) error
please help

 Dim bytBLOBData() As Byte = datTable.Rows(incount)("Pictu")
          Dim stmBLOBData As New IO.MemoryStream(bytBLOBData) 
            PictuPictureBox.Image = Image.FromStream(stmBLOBData)------parameter is not valid

Hi,Thanks Pgmer.
I have used another method, problem solved.

this is from your earlier conversation with Pgmer, i am stacking with the same problem can you show me tha another method you used?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.