This is a migrated thread and some comments may be shown as answers.

Upload & save to database help needed

2 Answers 86 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Andy Green
Top achievements
Rank 2
Andy Green asked on 26 Apr 2012, 08:20 PM
Hi all

This is not a Telerik issue, I just need some pointers.

Using the examples in the help, I have distilled the problem down to the following code, ie no error trapps or validation controls.

I get the error - Value of type '1-dimensional array of Byte' cannot be converted to 'Byte'

On the 'bytes' in the ModelImageAdd function. Why is this, what am I missing?
Protected Sub btnUpload_Click(sender As Object, e As System.EventArgs) Handles btnUpload.Click
 
 
        BindValidResults()
 
 
    End Sub
 
 
    Protected Sub BindValidResults()
 
        If RadUpload1.UploadedFiles.Count > 0 Then
 
            For Each file As UploadedFile In RadUpload1.UploadedFiles
                Dim bytes(file.ContentLength - 1) As Byte
                file.InputStream.Read(bytes, 0, file.ContentLength)
                 
            Dim a As New Model
                a.ModelImageAdd(Model_ID, file.GetName(), bytes, Owner_GUID)
            Next
 
        End If
    End Sub


The function to save the image data looks like this:
Public Function ModelImageAdd(ByVal _Model_ID As String, _ImageName As String, ByVal _ImageData As Byte, ByVal _Owner As Guid) As Boolean
    Try
        Using sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("cnModelDB").ToString)
            Using sqlCmd As New SqlCommand()
                With sqlCmd
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "ModelImageAdd"
                    .Connection = sqlConn
                    .Parameters.Add("@Model_ID", SqlDbType.Int).Value = _Model_ID
                    .Parameters.Add("@ImageName", SqlDbType.VarChar, 75).Value = _ImageName
                    .Parameters.Add("@ImageData", SqlDbType.Binary).Value = _ImageData
                    .Parameters.Add("@Owner", SqlDbType.UniqueIdentifier).Value = _Owner
                    .Parameters.Add("@ReturnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue
                    .Connection.Open()
                    .ExecuteScalar()
                End With
 
                If sqlCmd.Parameters("@ReturnValue").Value = 0 Then
                    ModelImageAdd = 1
                Else
                    ModelImageAdd = sqlCmd.Parameters("@ReturnValue").Value
                End If
            End Using
        End Using
    Catch ex As Exception
        ErrorLog.Log(String.Format("Model:ModelAdd(): {0}", ex.Message))
        Return False
    End Try
 
End Function

2 Answers, 1 is accepted

Sort by
0
Accepted
Peter Filipov
Telerik team
answered on 01 May 2012, 09:33 AM
Hi Andy,

I reviewed your code and found out what is causing the incorrect behavior. Please look at the BindValidResults() method. The bytes variable is declared as Byte type while it should be an array of bytes. You can review the following demo. In the Handler.ashx.vb file there is a method GetImageBytes() which could be helpful for you.

Kind regards,
Peter Filipov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Andy Green
Top achievements
Rank 2
answered on 03 May 2012, 09:42 AM
Thank you Peter.
Andy
Tags
Upload (Obsolete)
Asked by
Andy Green
Top achievements
Rank 2
Answers by
Peter Filipov
Telerik team
Andy Green
Top achievements
Rank 2
Share this question
or