Image data type and CLSA

Image data type and CLSA

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


Lyndon posted on Wednesday, September 26, 2007

Hi,

I'm currently developing an application in the CSLA framework that needs to include the possibility of a user providing an attachment which would be stored as image data in SQL Server 2005

In the code examples I've seen ( not CSLA ones), the process seems to be to load a files inputstream into a byte array and then using your normal parameters add with value insert into the database.

example

Dim Imagebytes(fileUpload1.PostedFile.InputStream.Length) As Byte

fileUpload1.Postedfile.Inputstream.Read (Imagebytes,0,imagebytes.length)

mycommand.paramaters.addwithvalue("@imagedata",imagebytes)

I easily understand that parameters.addwithvalue part of this should be located in the business object where the other equivalent commands go. But a couple of questions emerge on the rest.

1.Do the first two statements end up in the UI code? or is this violating keeping processing logic in the UI to a minimum? ie or should part of this be in the business object? Im assuming some basic stuff needs to remain in the UI because that is obviously where the application first has contact with what file its being asked to store...

2.When I try to put in the first of these commands in the UI, because of option strict on, I get a message about not being able to convert long to integer.  How do I avoid that to get around not having to turn off the option strict option?

Hope these arent dumb questions. My learning is evolutionary not revolutionary   ;-)

JoeFallon1 replied on Thursday, September 27, 2007

1. Yes - the first two lines of code are in the UI.

In my button handler I have code like this:

mBO.AttachDoc.contenttype = FileUpload.PostedFile.ContentType
mBO.AttachDoc.attdoc = GetBinaryData(FileUpload.PostedFile, FileUpload.PostedFile.ContentLength)

This is the function GetBinaryData - same as your 2 lines of code:

Private Function GetBinaryData(ByRef theFile As System.Web.HttpPostedFile, ByVal ContentLength As Integer) As Byte()
 
Dim Temp(ContentLength) As Byte
 
theFile.InputStream.Read(Temp, 0, ContentLength)
 
Return Temp
End Function

My BO has code like this to store the values:

Protected mAttdoc As Byte() = Nothing
Protected mContenttype As String = ""

Public Overridable Property Attdoc() As Byte()
 
Get
   
Return mAttdoc
 
End Get
 
Set(ByVal Value As Byte())
    mAttdoc = Value
    PropertyHasChanged(
"Attdoc")
 
End Set
End Property

Public Overridable Property Contenttype() As String
 
Get
   
Return mContenttype
 
End Get
 
Set(ByVal Value As String)
   
If mContenttype <> Value Then
     
mContenttype = Value
      PropertyHasChanged(
"Contenttype")
   
End If
 
End Set
End Property

2. I assume ImageBytes.Length is Long and FileUpload.PostedFile.ContentLength is an Integer.

a. You can use FileUpload.PostedFile.ContentLength directly to avoid the issue.

b. Just use CInt(ImageBytes.Length) if you don't want to change your code.

Joe

Lyndon replied on Thursday, September 27, 2007

Thanks Joe.

 

What you said makes sense to me. I kinda figured it should be something obvious. And I am trying to always pose the appropriate questions in my mind as to where code is properly kicked off when it could be in more than one place technically, but only in one place if best practices in business objects are followed.

Your information  continues to add more to my knowledge - its an evolution for me

Lyndon replied on Sunday, September 30, 2007

Joe,

Thanks for the previous information. I have some additional questions below. If you have time to respond- thanks! If not please just ignore this.

I included the CInt conversion and that seemed to get rid of the error that prevented a clean compile.

I am now getting the following error when it runs:

Operand type clash: tinyint is incompatible with image

Here is my code that attempts to get the data from the fileupload control and feed it to the business object

"Protected Sub ServiceRequestDataSource_InsertObject(ByVal sender As Object, _

ByVal e As Csla.Web.InsertObjectArgs) Handles UserServiceRequestDataSource.InsertObject

Dim obj As HRSM.Library.UserServiceRequest = _

HRSM.Library.UserServiceRequest.NewUserServiceRequest

Dim fileupload1 As FileUpload = CType(FormView1.Row.FindControl("fileupload1"), FileUpload)

Dim imagebytes(CInt(fileupload1.PostedFile.InputStream.Length)) As Byte

fileupload1.PostedFile.InputStream.Read(imagebytes, 0, imagebytes.Length)

Csla.Data.DataMapper.Map(e.Values, obj, "RequestID")

 

e.RowsAffected = SaveUserServiceRequest(obj)

End Sub "

Questions

1.Any idea on what the error message is indicating?

2. Im still not entirely clear on how the fileupload.inputstream gets associated with the attachment field that is part of the content included in the SaveUserServiceRequest call. I do bind the  fileuploadcontrol to the attachment field on the web page.Does that and the datamapper above ensure that the inputstream gets passed to the BO?

JoeFallon1 replied on Monday, October 01, 2007

 Byte

is NOT the same as 

Byte()

 

I believe the first is a tinyint and the second is an array of bytes.

Joe

Copyright (c) Marimer LLC