Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insery image to picture box from dataset

Status
Not open for further replies.

Appollo14

Technical User
Joined
Sep 4, 2003
Messages
182
Location
GB
Hi I'm a novice to programming and I'm trying to learn VB.Net and would appreciate a little help.

I'm working on a small project that has a contact database (Access) containing names, address's and a picture as an OLE Image.

I can get all of the text objects on my form with out a problem, however the image is not proving so easy!

Here is the code that I'm using to populate the fields and it is the last line that is giving me problems.


txtFirstName.Text = Ds.Tables("AddressBook").Rows(Inc).Item(1)
txtSurName.Text = Ds.Tables("AddressBook").Rows(Inc).Item(2)
txtAddress1.Text = Ds.Tables("AddressBook").Rows(Inc).Item(3)
txtAddress2.Text = Ds.Tables("AddressBook").Rows(Inc).Item(4)
txtAddress3.Text = Ds.Tables("AddressBook").Rows(Inc).Item(5)
txtPostCode.Text = Ds.Tables("AddressBook").Rows(Inc).Item(6)
txtPhone.Text = Ds.Tables("AddressBook").Rows(Inc).Item(7)
txtEmail.Text = Ds.Tables("AddressBook").Rows(Inc).Item(8)
txtNotes.Text = Ds.Tables("AddressBook").Rows(Inc).Item(9)
bxImage.Image = Ds.Tables("AddressBook").Rows(Inc).Item("image")

When it runs I get an exception saying Invalid Cast.

Could someone please point me in the right direction.

Many thanks,
Noel.
 
Are you sure that it is the image that s stored in the access database? Or is it the path to the image?

try this
when it is stored in the database

Code:
bxImage.Image = Image.fromstream(Ds.Tables("AddressBook").Rows(Inc).Item("image"))

else

Code:
bxImage.Image = Image.fromfile(Ds.Tables("AddressBook").Rows(Inc).Item("image"))



Christiaan Baes
Belgium

"My new site" - Me
 
Hi Chrisstiaan, thanks for the reply.

Yes the image is stored in the database, i'm tring to create a server app and thought that doing it that would be tidy.

I tried your code and I still get the same error

"System.InvalidCastException' occurred in AddrdessAppNav.exe
Additional information: Specified cast is not valid."

Any other thoughts.

Regards,
Noel.
 
So it need a bit more work

wich I found on this site.


Code:
'if we got here we are good to go. Create the buffer and fill it 
Dim buffer() As Byte = CType(Ds.Tables("AddressBook").Rows(Inc).Item("image"), Byte()) 
'create memory stream from this array of bytes 
Dim str As New MemoryStream(buffer) 

'set the image property of the picture box to the image in the stream 
bxImage.Image = Image.FromStream(str)

Christiaan Baes
Belgium

"My new site" - Me
 
Thanks for the reply again Christiaan,

I've been busy with sql db triggers so i've not had a chance to try it out yet but I'm going to have a go now and i'll let you know how i get on.

Thanks again for your help.

Regards,
Noel.
 
Hi Christiaan,

Just a quick note to let you know that after changing the format of the images that i'd stored (from OLE to Long Binary) that the code you supplied worked a treat.

Once again, many thanks for your help.

Regards,
Noel.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top