locked
value of type '1-dimensional array of byte ' cannot be converted to 'Byte' RRS feed

  • Question

  • User2049634312 posted

    Hi

     Im having some issues with a , i thought, simple app to save an image as binary into a db.

     I am using the code below which was originally C and ive tried to convert to VB.

     I keep getting the error ''value of type '1-dimensional array of byte ' cannot be converted to 'Byte' '' on this line of code

    Dim rowsaffected As Integer = SaveToDB(imgname, imgbinarydata, imgcontenttype)

     

    my full code is this:

      

     Private Sub SaveToDB(ByVal imgname As String, ByVal imgbin As Byte, ByVal imgcontenttype As String) as integer
    
            Dim conn As New SqlConnection("Datasource=.\SQLEXPRESS;AttachDbFilename=C:\Users\The Bloodworth's\Documents\Visual Studio 2008\Projects\Binary\Binary\App_Data\binary.mdf")
            Dim sql As String = "INSERT INTO image (img_name, img_data, img_contenttype) VALUES (@img_name, @img_data, @img_contenttype)"
            Dim cmd As New SqlCommand(sql, conn)
    
            Dim param0 As New SqlParameter("@img_name", SqlDbType.VarChar, 50)
            param0.Value = imgname
            cmd.Parameters.Add(param0)
    
            Dim param1 As New SqlParameter("@img_data", SqlDbType.Image)
            param1.Value = imgbin
            cmd.Parameters.Add(param1)
    
            Dim param2 As New SqlParameter("@img_contenttype", SqlDbType.VarChar, 50)
            param2.Value = imgcontenttype
            cmd.Parameters.Add(param2)
    
            conn.Open()
            Dim numrowsaffected As Integer = cmd.ExecuteNonQuery
            conn.Close()
            MsgBox(numrowsaffected)
    
        End Sub
    
    
        Protected Sub UploadBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles UploadBtn.Click
    
            If Page.IsValid Then
                Dim imgStream As System.IO.Stream = UploadFile.PostedFile.InputStream
                Dim imglen As Integer = UploadFile.PostedFile.ContentLength
                Dim imgcontenttype As String = UploadFile.PostedFile.ContentType
                Dim imgname As String = txtImgName.Value
    
                If imglen > 0 Then
                    Dim imgbinarydata(imglen - 1) As Byte
                    Dim n As Integer = imgStream.Read(imgbinarydata, 0, imglen)
                    Dim rowsaffected As Integer = SaveToDB(imgname, imgbinarydata, imgcontenttype)
                End If
    
            End If
    
        End Sub

     wondered if anyone could help

     

    many thanks

    Sunday, April 5, 2009 1:53 PM

Answers

  • User1439985827 posted

     I think Mike meant for his code to be a Function, not a Sub:

    Private Function SaveToDB(ByVal imgname As String, ByVal imgbin() As Byte, ByVal imgcontenttype As String) As Integer
    
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 5, 2009 2:57 PM

All replies

  • User-158764254 posted

    define your method like this:

    Private Sub SaveToDB(ByVal imgname As String, ByVal imgbin() As Byte, ByVal imgcontenttype As String) as integer
    

     

    note the extra parenthesis.

    Sunday, April 5, 2009 2:12 PM
  • User2049634312 posted

    many thanks for the response - it has removed that error but now on this line the underlined bit

     

    Dim rowsaffected As Integer = SaveToDB(imgname, imgbinarydata, imgcontenttype)

     

    says 'Expression does not produce a value'

     

    cheers

    Sunday, April 5, 2009 2:51 PM
  • User1439985827 posted

     I think Mike meant for his code to be a Function, not a Sub:

    Private Function SaveToDB(ByVal imgname As String, ByVal imgbin() As Byte, ByVal imgcontenttype As String) As Integer
    
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 5, 2009 2:57 PM
  • User2049634312 posted

    fixed it - it was still from the conversion of c that it was wanting the numrowsaffected to be return from the savedb sub!

     

     

    Sunday, April 5, 2009 2:58 PM