Click here to Skip to main content
15,069,297 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
i have problem in retrieving picture in picturebox

i have a dataset named "DS" and i have built memberPhotoTableAdapter in it.

i call the MemberPhotoTableAdapter in form load by this code:

Me.MembersPhotoTableAdapter.FillBy(Me.DS.MembersPhoto, NationalCode.ToString())

this is the codes to insert picture in database :


VB
Private Sub btnSavePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSvePic.Click

        Try
            Dim ms As New MemoryStream()
            Photo.Image.Save(ms, Photo.Image.RawFormat)
            Dim arrPic As Byte() = ms.GetBuffer()
            ms.Close()
            MembersPhotoTableAdapter.InsertQuery(arrPic, NationalCode.ToString())
            MsgBox("pic saved")

        Catch ex As Exception
            MsgBox("Error")
        End Try
    End Sub
this code works well,but for retrieving the picture i have problem.

this is the code for retrieving picture in form_Load:

VB
Dim arrPic As Byte()
       arrPic = DirectCast(DS.Tables("MembersPhoto").Rows(0)("pic"),Byte())
       Dim ms As MemoryStream = New MemoryStream(arrPic)
       Photo.Image = New Bitmap(ms)


i know that the bold line is wrong and in running time shows error, for the persons that i have inserted picture for them before, shows this error:

error: the parameter is not valid

and for the persons without picture showes this error:
error : there is no row in position 0.

but what shuold i write insted of that



thanks alot
Posted
Updated 15-Aug-12 11:18am
v5
Comments
[no name] 15-Aug-12 16:15pm
   
It is really helpful to know what the error is that you are getting....
setareh_sky 15-Aug-12 16:39pm
   
for the persons that i have inserted picture for them before, shows this error:

error: the parameter is not valid

and for the persons without picture showes this error:
error : there is no row in position 0.
[no name] 15-Aug-12 16:18pm
   
Try arrPic = DirectCast(DS.Tables("MembersPhoto").Rows(0).Item("pic"), Byte())
Kenneth Haugland 15-Aug-12 16:39pm
   
Are you sure that there are items in your datatable?
setareh_sky 15-Aug-12 16:47pm
   
i knowe it's wrong
actually, i want to show the picture of each person which its NationalCode is the same as the NationalCode that i have fillBy the tableAdapter with it in form_load.

in the MembersPhoto table i have two column :pic , NationalCode
Kenneth Haugland 15-Aug-12 16:50pm
   
I woul dcreate a class called person witch has the properties Picture, NationalCode, And personal inforamtion.

1 solution

The most likely reason is that the problem is not with the reading out of the database, but with the saving into the database:
VB
Photo.Image.Save(ms, Photo.Image.RawFormat)
Try changing it to
VB
Photo.Image.Save(ms, ImageFormat.Jpeg)
   
Comments
setareh_sky 16-Aug-12 3:58am
   
no,saving command works correctly
OriginalGriff 16-Aug-12 4:18am
   
But what is it saving?
The normal reason you get a "the parameter is not valid" error is because the bytes-to-image converter does not understand the format of the data you are giving it. Since you are getting this error, the most likely cure is to save the data in a format it can understand. While you may be saving data perfectly happily, that does not mean that the data is in a readable format!
setareh_sky 16-Aug-12 4:32am
   
are you sure that the **Photo.Image.Save(ms, ImageFormat.Jpeg) ** works?
"ImageFormat.Jpeg" doesnt exist !!!!!!!
what nameSapce should i add?
OriginalGriff 16-Aug-12 4:45am
   
System.Drawing.Imaging.ImageFormat.Jpeg

If you aren't sure, VS usually puts a small underline near the beginning of the name when your text cursor is in the word. Hover the mouse over the underline and it gives you a drop down with options.
setareh_sky 16-Aug-12 5:02am
   
i know about the drop down list
but your soulution doesnt solve the problem

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