Upload & save to database help needed

Thread is closed for posting
3 posts, 1 answers
  1. Andy Green
    Andy Green avatar
    376 posts
    Member since:
    Aug 2008

    Posted 26 Apr 2012 Link to this post

    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
        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)
            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
            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
                    End With
                    If sqlCmd.Parameters("@ReturnValue").Value = 0 Then
                        ModelImageAdd = 1
                        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. Answer
    Peter Filipov
    Peter Filipov avatar
    1028 posts

    Posted 01 May 2012 Link to this post

    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.
  3. Andy Green
    Andy Green avatar
    376 posts
    Member since:
    Aug 2008

    Posted 03 May 2012 Link to this post

    Thank you Peter.
Back to Top