im trying to create a simple form of saving an image as binary data to a db.

i never done work in this area and have never played with 'byte' types

Im working fro a tutorial in C and while converting i keep getting the error 'type 'byte' has no constructors vb.net' on this line:

Dim imgbinarydata As Byte() = New Byte(imglen)

my full code is:

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
            Dim imgbinarydata As Byte() = New Byte(imglen)

            Dim n As Integer = imgStream.Read(imgbinarydata, 0, imglen)

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


        End If

    End Sub

        End If

wondered if anyone could help

many thanks

Correct syntax is Dim imgbinarydata(imglen - 1) As Byte Since arrays are zero-based, you use imglen - 1 to get an array with imglen elements.

You should check that UploadFile.PostedFile.ContentLength is not zero to avoid errors:

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

Hi

many thanks for your response

i now get the error 'value of type '1-dimensional array of byte ' cannot be converted to 'Byte' ' on this line:

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

savetodb procedure

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

many thanks

Now, instead of a single byte, you have to pass a byte array to procedure Private Function SaveToDB(ByVal imgname As String, ByVal imgbin As Byte(), ByVal imgcontenttype As String) as integer Notice that this is a function not a sub!

Your current code does not return any value i.e. number of rows affected. Here's a fix for that and this also ensures that the db-connection gets closed

Dim numrowsaffected As Integer
conn.Open()
Try
  numrowsaffected = cmd.ExecuteNonQuery
Catch ex As Exception
  ' Handle error(s)
Finally
  conn.Close()
End Try
Return numrowsaffected
This article has been dead for over six months. Start a new discussion instead.