By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

storing image in Access table

P: n/a
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.

--
Message posted via http://www.dotnetmonster.com
Feb 8 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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?...6-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" <u16975@uwe> schreef in bericht
news:5b8e90a30b2ec@uwe...
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.

--
Message posted via http://www.dotnetmonster.com

Feb 8 '06 #2

P: n/a
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

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...b-net/200602/1
Feb 8 '06 #3

P: n/a
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
Feb 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.