Business Objects and Image Columns

Business Objects and Image Columns

Old forum URL: forums.lhotka.net/forums/t/5048.aspx


raz0rf1sh posted on Wednesday, July 02, 2008

I'm banging my head on this one ... and previous posts aren't helping.  I am trying to store a document in the database, and from what I can tell "something" is in there ... it says <binary> in the column when I view the record in SQL Server.

This is the code I am using to read the document and set it my property:

            FileInfo fi = new FileInfo(Properties.Settings.Default.TestImage);
            FileStream fs = fi.Open(FileMode.Open, FileAccess.Read);

            BinaryReader br = new BinaryReader(fs);

            byte[] data = br.ReadBytes(m_TestObject.FileSize);

            m_TestObject.FileData = data;

That seems to work, but when I try to access the data, my property value is null.  Here is the code I am using to read in the image:

            dr.GetBytes("FileData", 0, m_FileData, 0, int.MaxValue);

This does not error out, again, it just doesn't seem to read anything.  Now this could be an image, document or whatever file type ... this example is for an image.

On a side note, I tried viewing the contents of the field in a Form, just to see what was in there ... and it didn't display the fact that it was an image in there ... so maybe I'm not writing it correctly.

Any help would be GREATLY appreciated!

raz0rf1sh replied on Wednesday, July 02, 2008

Okay ... I figured my own problem out ... was doing A LOT of things incorrect!!!

Here is how I wanted to set the property value:

            FileInfo fi = new FileInfo(Properties.Settings.Default.TestImage);
            FileStream fs = fi.Open(FileMode.Open, FileAccess.Read);

            m_TestObject.Name = System.IO.Path.GetFileNameWithoutExtension(fi.Name);
            m_TestObject.FileName = fi.Name;
            m_TestObject.FileSize = (int)fi.Length;

            BinaryReader br = new BinaryReader(fs);

            Byte[] data = new Byte[(int)fs.Length];

            fs.Read(data, 0, (int)fs.Length);

            m_TestObject.FileData = data;

            fs.Close();

My read in my business objects was changed to:

            m_FileData = new Byte[dr.GetBytes("FileData", 0, null, 0, int.MaxValue)];
            dr.GetBytes("FileData", 0, m_FileData, 0, m_FileData.Length);

And the "rebuilding", in this case, of the image is:

            string destination = string.Empty;

            destination = String.Format("{0}_Restored.{1}", m_TestObject.Name, System.IO.Path.GetExtension(m_TestObject.FileName));

            fs = new FileStream(destination,
                FileMode.OpenOrCreate, FileAccess.Write);

            fs.Write(m_TestObject.FileData, 0, m_TestObject.FileSize);
           
            fs.Close();

            fi = new FileInfo(destination);
            fs = fi.Open(FileMode.Open, FileAccess.Read);

            this.pictureBox1.Image = System.Drawing.Image.FromStream(fs);
   
            fs.Close();

Hope this saves someone a few hairs!!!

JoeFallon1 replied on Wednesday, July 02, 2008

Protected mAttdoc As Byte() = Nothing

Public Overridable Property Attdoc() As Byte()
Get
Return mAttdoc
End Get

Set(ByVal Value As Byte())
mAttdoc = Value
PropertyHasChanged(
"Attdoc")
End Set

End Property

Protected Overridable Sub FetchData(ByVal dr As SafeDataReader)
With dr
If .Read Then
  mAttdoc = CType(.Item("attdoc"), Byte())
  mContenttype = Trim(.GetString(
"contenttype"))
End If
End With
End Sub

For writing to the DB I use a parameterized SQL statement.

In DP_Update I have code like this:
Dim pAttdoc As IDataParameter = DAL.CreateParameter("attdoc"
, mAttdoc)
Dim pContentType As IDataParameter = DAL.CreateParameter("contenttype", mContenttype)

DAL.ExecuteNonQuery(tr, CommandType.Text, AttachDocDAO.Update(
pAttdoc, pContentType)

Where AttachDocDAO.Update is the string:
 
"UPDATE myDBtable SET attdoc=@attdoc,contenttype=@contenttype"

Joe

 

Copyright (c) Marimer LLC