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
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
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
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203