locked
Conversion from type 'Byte()' to type 'Byte' is not valid. RRS feed

  • Question

  • Protected Function SavePostedFile(ByVal uploadedFile As UploadedFile) As String
            Dim ret As String = ""
            If uploadedFile.IsValid Then
                Dim uc As Stream = UploadControl.PostedFile.InputStream
                Dim br As New BinaryReader(uc)
                Dim bytes As Byte() = br.ReadBytes(uc.Length)
                Session("a") = bytes

                Dim tempFileName As String = MapPath(UploadDirectory) + Format(DateTime.Now, "yyMd_hmmss") + "_" + uploadedFile.FileName
                uploadedFile.SaveAs(tempFileName)
                If Not CInt(Me.Request.QueryString("iPK_DokumenSokongan")) = 0 Then
                    Dim xDokumenSokonganLoad2 As New XDokumenSokongan
                    xDokumenSokonganLoad2.LoadRow(Me.Request.QueryString("iPK_DokumenSokongan"))
                    xDokumenSokonganLoad2.Entity.cNamaLampiran = Format(DateTime.Now, "yyMd_hmmss") + "_" + uploadedFile.FileName
                    xDokumenSokonganLoad2.Entity.cData = Session("a")
                    xDokumenSokonganLoad2.SaveRow()
                ElseIf Me.Request.QueryString("Upload") = "New" Then
                    Dim xDokumenSokonganLoad2 As New XDokumenSokongan
                    xDokumenSokonganLoad2.NewRow()
                    xDokumenSokonganLoad2.Entity.cNamaLampiran = Format(DateTime.Now, "yyMd_hmmss") + "_" + uploadedFile.FileName
                    xDokumenSokonganLoad2.Entity.iFK_KodJenisDokumenSokongan = Constants.DokumenSokongan_Lain
                    xDokumenSokonganLoad2.Entity.iFK_LesenButiran = CInt(Session("FK_LesenButiran"))
                    xDokumenSokonganLoad2.Entity.cData = Session("a")
                    xDokumenSokonganLoad2.SaveRow()
                    Session("PK_DokumenSokongan") = xDokumenSokonganLoad2.Entity.iPK_DokumenSokongan
                End If

                If Not CInt(Me.Request.QueryString("iPK_ButiranIklan")) = 0 Then
                    Dim butiraniklan2 As New MButiranIklan
                    butiraniklan2.LoadRow(Me.Request.QueryString("iPK_ButiranIklan"))
                    butiraniklan2.Entity.cLampiranVisual = Format(DateTime.Now, "yyMd_hmmss") + "_" + uploadedFile.FileName
                    butiraniklan2.Entity.cLampiranLokasi = Format(DateTime.Now, "yyMd_hmmss") + "_" + uploadedFile.FileName
                    butiraniklan2.SaveRow()
                End If
            End If
            Return ret
        End Function

    column cData data type is varbinary(MAX).

    Tuesday, September 23, 2014 2:15 AM

Answers

  • Sorry. I try my best to explain my problem that is why I post a giant "wall of code" here, and my English not good. So thanks to everyone for helping me. It helps me a lot on dealing with forum and give me idea to solve my problem. Finally I can save an image to database. I found out that I created wrong entity type.

    From:

    Public Property cData() As Byte{

    ...

    }

    To:

    Public Property cData() As Byte(){

    ...

    }

    • Marked as answer by Redzuan Mohamed Wednesday, September 24, 2014 4:49 AM
    Wednesday, September 24, 2014 4:49 AM

All replies

  • A Byte() is an array of Bytes. So it can not be converted to a single Byte.

    Each Byte in the array of Bytes is a Byte though.


    La vida loca

    Tuesday, September 23, 2014 2:22 AM
  • column cData data type is varbinary(MAX).

    Presumably it is this line:
        Session("a") = bytes

    The message s telling your that an array of bytes cannot be saved into that variable.  Some type of conversion will be required.  For instance, perhaps the bytes represent ASCII values in which case they need to be converted to string where each byte is one character in the string.

    • Edited by Acamar Tuesday, September 23, 2014 2:25 AM
    Tuesday, September 23, 2014 2:24 AM
  • this line:

    xDokumenSokonganLoad2.Entity.cData = Session("a")

    I want to save into column cData. But the value need to be converted. Can show me how to convert byte() to byte or have any suggestion?

    Tuesday, September 23, 2014 2:43 AM
  • Can show me how to convert byte() to byte or have any suggestion?

    In order to convert the array of bytes into a value you need to know what that array represents and how it has been encoded.  It could be anything from an integer to a string to an image.  The conversion will be different for each type.

    Tuesday, September 23, 2014 3:17 AM
  • Hi,

    i managed to convert byte() to string using this

                   xDokumenSokonganLoad3.Entity.cData = BitConverter.ToString(bytes)

    but it cannot save into varbinary(max) column. So i changed the column datatype to varchar(max)

    and it can save. Here are the data saved :

    FF-D8-FF-E0-00-10-4A-46-49-46....

    is it can be accepted? can the data convert back as image?

    Tuesday, September 23, 2014 9:48 AM
  • Hi,

    i managed to convert byte() to string using this

                   xDokumenSokonganLoad3.Entity.cData = BitConverter.ToString(bytes)

    but it cannot save into varbinary(max) column. So i changed the column datatype to varchar(max)

    and it can save. Here are the data saved :

    FF-D8-FF-E0-00-10-4A-46-49-46....

    is it can be accepted? can the data convert back as image?

    I don't really understand what you conceive as English as it makes mostly no sense to me.

    Option Strict On
    
    Imports System.IO
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.Image = Image.FromFile("C:\Users\John\Desktop\Picture Files\Moon2.Png")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim PBox1Image As New MemoryStream()
            PictureBox1.Image.Save(PBox1Image, System.Drawing.Imaging.ImageFormat.Png)
            Dim PicBox1ByteArray() As Byte = PBox1Image.GetBuffer()
            PBox1Image.Dispose()
    
            For Each Item In PicBox1ByteArray
                RichTextBox1.AppendText(Item.ToString & "-")
            Next
    
            Dim PBox2ByteList As New List(Of Byte)
            Dim RTB1Split() As String = RichTextBox1.Text.Split("-"c)
            For Each Item In RTB1Split
                If Item <> "" Then
                    PBox2ByteList.Add(CByte(Item))
                End If
            Next
    
            Dim PBox2Image As New MemoryStream(PBox2ByteList.ToArray)
            PictureBox2.Image = Image.FromStream(PBox2Image)
            PBox2Image.Dispose()
    
        End Sub
    
    End Class
    


    La vida loca

    Tuesday, September 23, 2014 6:42 PM
  • Please don't post a giant "wall of code" and expect us to figure out what is wrong.  At least tell us what line the error occurs on! 
    Tuesday, September 23, 2014 8:59 PM
  • i managed to convert byte() to string using this

                   xDokumenSokonganLoad3.Entity.cData = BitConverter.ToString(bytes)

    but it cannot save into varbinary(max) column. So i changed the column datatype to varchar(max)

    You can't just convert the byte array into anything you like: you have to choose the conversion based on what you are trying to do.

    If all you are trying to do is store the byte array into a data field, then you need to define a data field that can accept an array of bytes.   How you do this depends on the database that you are using.

    If the byte array represents something that can be stored as a particular database type, such as text, then you can convert it to that type and save into a field defined as text.

    It all depends on what you are actually trying to do.

    Tuesday, September 23, 2014 11:11 PM
  • Sorry. I try my best to explain my problem that is why I post a giant "wall of code" here, and my English not good. So thanks to everyone for helping me. It helps me a lot on dealing with forum and give me idea to solve my problem. Finally I can save an image to database. I found out that I created wrong entity type.

    From:

    Public Property cData() As Byte{

    ...

    }

    To:

    Public Property cData() As Byte(){

    ...

    }

    • Marked as answer by Redzuan Mohamed Wednesday, September 24, 2014 4:49 AM
    Wednesday, September 24, 2014 4:49 AM