0

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
4
Contributors
17
Replies
39
Views
5 Years
Discussion Span
Last Post by fredrick_3
0

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
0

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
0

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
0

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

Edited by Dani: Formatting fixed

0
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.

0

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

Edited by prathapsv3: n/a

0

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

0

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

0

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

0

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

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

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?

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.