Set parameter value to "Insert Parameters"

calvin

Regular
Joined
Nov 6, 2003
Messages
71
Hi,

I am using objectdatasource as datasource in page. I use the "File Upload" control to get the image and save to database. Within the database table, there are a lot of datafield. I got the problem of setting the parameter value for the image. I use three datafield to store the image relevant data.

Datafield DataType
1. imgName (nvarchar 50)
2. imgData (Image)
3. imgType (nvarchar 50)
Visual Basic:
Try
            Dim imgName As String
            Dim imgContentType As String
            Dim imgLen As Int32
            Dim imgbin() As Byte

If Me.FileUpload.HasFile = True Then
   If Me.FileUpload.PostedFile.FileName.Trim.Length > 0 And  Me.FileUpload.PostedFile.ContentLength > 0 Then
                    Dim imgStream As Stream = Me.FileUpload.PostedFile.InputStream()
                    imgLen = Me.FileUpload.PostedFile.ContentLength
                    imgContentType = Me.FileUpload.PostedFile.ContentType
                    imgName = Me.FileUpload.PostedFile.FileName.Substring(Me.FileUpload.PostedFile.FileName.LastIndexOf("\") + 1)
                    Dim imgBinaryData(imgLen) As Byte
                    'Dim n As Int32 = imgStream.Read(imgBinaryData, 0, imgLen)
                    imgbin = imgBinaryData

 ObjectDataSource.InsertParameters("imgName") = imgName
 ObjectDataSource.InsertParameters("imgData") = imgbin
 ObjectDataSource.InsertParameters("imgType") = imgContentType 
 ObjectDataSource.Insert()
Question
1: How can I set the "value" to the parameter?
2. Do I make mistake to the image field, since declare in database is "Image" datatype and use "Byte" in codebehind?
3: For next time if user want to retrieve the image, can it be "restore" as "image file"?
4. Can I use the "File Upload" control in "DetailsView" with objectdatasource? Because I have try to do it, but unsuccessful. Since I use the "Template Field" and drag the "File Upload" control to the "InsertItemTemplate", but there is no bound property of "text" which to bound to the "field binding". So, is this possible to use "file upload" control in DetailsView?? If so, how to do It.
5. With the approach in question 4, the DetailsView was include in the "atlas:UpdatePanel", will this make error?

Appreciate for those giving solution and help.

Calvin
 
Last edited by a moderator:
Get Solution from others

Q_Quek wrote:
1: How can I set the "value" to the parameter?

You can set the value in the Selecting event handler of the ObjectDataSource control.

Q_Quek wrote:
2. Do I make mistake to the image field, since declare in database is "Image" datatype and use "Byte" in codebehind?

No.

Q_Quek wrote:
3: For next time if user want to retrieve the image, can it be "restore" as "image file"?

Yes. You simply create a seperate web page or custom http handler for showing up an image store in DB, you can search for the sample code out there or on this site.

Q_Quek wrote:
4. Can I use the "File Upload" control in "DetailsView" with objectdatasource? Because I have try to do it, but unsuccessful. Since I use the "Template Field" and drag the "File Upload" control to the "InsertItemTemplate", but there is no bound property of "text" which to bound to the "field binding". So, is this possible to use "file upload" control in DetailsView?? If so, how to do It.

Yes, you can. You can define a select parameter for the ObjectDataSource control like this:

<asp:Parameter Name="imgData" Type="Object" />
In the Selecting event handler of the ObjectDataSource, you can get the binary data of the uploaded image from the FileUpload control, then set it to the select parameter in the InputParameters collection:

protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e) { FileUpload upload = DetailsView1.FindControl("FileUpload") as FileUpload; using (BinaryReader reader = new BinaryReader(upload.PostedFile.InputStream)) { byte[] bytes = new byte[upload.PostedFile.ContentLength]; reader.Read(bytes, 0, bytes.Length); e.InputParameters["imgData"] = bytes; } }

Q_Quek wrote:
5. With the approach in question 4, the DetailsView was include in the "atlas:UpdatePanel", will this make error?

Yes, it may raise the error since the file does not get uploaded when you make an out-of-band request with Atlas, you need to submit the form to upload the image.
 
Back
Top