storing image in Access table

A

anewbie

hi.

ive been searching for help with storing images in access. after much
hunting i found this bit of code that Cor Ligthert put up on another forum.
but when i try to update to the dataset, i get this error: system.argument.
exception in system.data.dll, cannot change data type of a column once it has
data.

for one thing my column is empty, for another, i get that message even when i
select "upload to dataset" without first selecting an image.

can someone please help?

\\\it needs a picturebox and four buttons on a page.
Private abyt() As Byte
Private fo As New OpenFileDialog
Private sf As New SaveFileDialog

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Reading a picture and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button2.Click
'writing a picture from a bytearray
If sf.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(sf.FileName, _
IO.FileMode.CreateNew)
Dim bw As New IO.BinaryWriter(fs)
bw.Write(abyt)
bw.Close()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button3.Click
'writing a bytearray to a dataset
Dim ds As New DataSet
ds.Tables.Add(New DataTable("Photo"))
ds.Tables(0).Columns.Add(New DataColumn("Sample"))
ds.Tables(0).Columns(0).DataType =
System.Type.GetType("System.Byte[]")
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
ds.Tables(0).Rows(0)(0) = abyt
Dim sf As New SaveFileDialog
If sf.ShowDialog = DialogResult.OK Then
ds.WriteXml(sf.FileName, XmlWriteMode.WriteSchema)
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
'reading a picture from a dataset
Dim ds As New DataSet
If fo.ShowDialog = DialogResult.OK Then
ds.ReadXml(fo.FileName)
End If
abyt = CType(ds.Tables(0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End Sub
///

Cor

thanks.
 
C

Cor Ligthert [MVP]

Anewbie,

I tested the sample again and could not find really errors. (withouth a part
that could go wrong in wrong sequence (try to read a dataset and than
abording), I changed that).

http://www.vb-tips.com/default.aspx?ID=0bf3f72d-b722-459d-8a46-38b5a2f7fdf0

However I have the idea that your problem has not to do with this, but just
with reading and writing a dataset to/from an access database. Can you
explain it a little bit more.

Cor


However in my idea
anewbie @ VB.NET via DotNetMonster.com said:
hi.

ive been searching for help with storing images in access. after much
hunting i found this bit of code that Cor Ligthert put up on another
forum.
but when i try to update to the dataset, i get this error:
system.argument.
exception in system.data.dll, cannot change data type of a column once it
has
data.

for one thing my column is empty, for another, i get that message even
when i
select "upload to dataset" without first selecting an image.

can someone please help?

\\\it needs a picturebox and four buttons on a page.
Private abyt() As Byte
Private fo As New OpenFileDialog
Private sf As New SaveFileDialog

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Reading a picture and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button2.Click
'writing a picture from a bytearray
If sf.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(sf.FileName, _
IO.FileMode.CreateNew)
Dim bw As New IO.BinaryWriter(fs)
bw.Write(abyt)
bw.Close()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button3.Click
'writing a bytearray to a dataset
Dim ds As New DataSet
ds.Tables.Add(New DataTable("Photo"))
ds.Tables(0).Columns.Add(New DataColumn("Sample"))
ds.Tables(0).Columns(0).DataType =
System.Type.GetType("System.Byte[]")
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
ds.Tables(0).Rows(0)(0) = abyt
Dim sf As New SaveFileDialog
If sf.ShowDialog = DialogResult.OK Then
ds.WriteXml(sf.FileName, XmlWriteMode.WriteSchema)
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
'reading a picture from a dataset
Dim ds As New DataSet
If fo.ShowDialog = DialogResult.OK Then
ds.ReadXml(fo.FileName)
End If
abyt = CType(ds.Tables(0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End Sub
///

Cor

thanks.
 
A

anewbie

hi! thanks for the help... i dont understand - why am i alone getting that
error! hmm. strange. anyhow, my problem is this: im creating a database for
managing personnel records, and id like to have the employees passport sized
photograph displayed in the main form.

im able to store the file path properly, but i dont understand this image
memory streaming bit. i was told theres an image load option in vb... would i
be able to use that to upload, save and display my images?

thanks
 
C

Cor Ligthert [MVP]

Anewbie,

I thought that the most problems were with Access in this the type of the
datafield.

I thought it was something as binary.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top