locked
Convert C# to VB (Upload Image to DB) RRS feed

  • Question

  • User-2089506584 posted

    Hi,

    product.ImageMimeType = image.ContentType;
    product.ImageData = new byte[image.ContentLength];
    image.InputStream.Read(product.ImageData, 0, image.ContentLength);

    I got the following error when converting to VB.

    Function Index(ByVal product As Product, ByVal image As HttpPostedFileBase) As ActionResult
    'Value of type 'String' cannot be converted to 'System.Data.Linq.Binary'.
                product.ImageMimeType = imagefile.ContentType

                product.ImageData = Convert.ToByte(imagefile.ContentLength)

    'Value of type 'String' cannot be converted to '1-dimensional array of Byte'.    

                imagefile.InputStream.Read(product.ImageData, 0, imagefile.ContentType)

        Return True
    End Function

    DB Definition
    ImageData (varchar(MAX))
    ImageMiMeType (varbinary(50))

    Thursday, September 17, 2009 1:25 AM

Answers

  • User-1363023537 posted

    Hi,
    you can try this simple article  http://www.revenmerchantservices.com/page/asp-net-image-upload.aspx 
    to upload image into DB

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2009 4:23 AM
  • User397347636 posted

    The conversion of that C# code is:

    product.ImageMimeType = image.ContentType
    product.ImageData = New Byte(image.ContentLength - 1){}
    image.InputStream.Read(product.ImageData, 0, image.ContentLength)


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2009 10:19 AM
  • User1839833660 posted

    You can convert C# code to VB using link below

    http://www.developerfusion.com/tools/convert/csharp-to-vb/

    You may also visit link below

    Save Images to DataBase in ASP.NET

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2009 10:27 AM
  • User-1542018982 posted

    Hi,

    May be you can consider to use  FileBytes property of FileUpload control for ImageData(if your dataType is binary)

    For details, you may refer link below:
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filebytes.aspx

    I think your property data type does not match with type you want to assigned.

    product.ImageMimeType: Binary Datatype?
    product.ImageData: String Datatype?

    If yes, then you properly have set the wrong dataType.

    I think it should be datatype below in order for you to assign correct property.
    product.ImageMimeType: String Datatype
    product.ImageData: Binary Datatype

    Hope I understand correct and hope it help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2009 9:12 PM

All replies

  • User-1363023537 posted

    Hi,
    you can try this simple article  http://www.revenmerchantservices.com/page/asp-net-image-upload.aspx 
    to upload image into DB

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2009 4:23 AM
  • User397347636 posted

    The conversion of that C# code is:

    product.ImageMimeType = image.ContentType
    product.ImageData = New Byte(image.ContentLength - 1){}
    image.InputStream.Read(product.ImageData, 0, image.ContentLength)


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2009 10:19 AM
  • User1839833660 posted

    You can convert C# code to VB using link below

    http://www.developerfusion.com/tools/convert/csharp-to-vb/

    You may also visit link below

    Save Images to DataBase in ASP.NET

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2009 10:27 AM
  • User-2089506584 posted

    1. product.ImageData = New Byte(image.ContentLength - 1){}  


    I get this error "Value of type '1-dimensional array of Byte' cannot be converted to 'String'." [:(]

    Thursday, September 17, 2009 9:04 PM
  • User-1542018982 posted

    Hi,

    May be you can consider to use  FileBytes property of FileUpload control for ImageData(if your dataType is binary)

    For details, you may refer link below:
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filebytes.aspx

    I think your property data type does not match with type you want to assigned.

    product.ImageMimeType: Binary Datatype?
    product.ImageData: String Datatype?

    If yes, then you properly have set the wrong dataType.

    I think it should be datatype below in order for you to assign correct property.
    product.ImageMimeType: String Datatype
    product.ImageData: Binary Datatype

    Hope I understand correct and hope it help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2009 9:12 PM
  • User-2089506584 posted

    May be you can consider to use  FileBytes property of FileUpload control.

    I'm working under MVC environment. I'm not using any server controls.

    <ValidateInput(False), AcceptVerbs(HttpVerbs.Post)> _

    Function Index(ByVal product As Product, ByVal image As HttpPostedFileBase) As ActionResult

                product.ImageMimeType = imagefile.ContentType
                product.ImageData = New Byte(image.ContentLength - 1){} 

                imagefile.InputStream.Read(product.ImageData, 0, imagefile.ContentType)

        Return True
    End Function

    Thursday, September 17, 2009 9:20 PM
  • User-1542018982 posted

    Hi,

    I think your property data type does not match with type you want to assigned.

    product.ImageMimeType: Binary Datatype?
    product.ImageData: String Datatype?

    If yes, then i think you properly have wrong dataType or assigning wrongly.

    ContentType of HttpPostedFileBase return String. So, error will occur when convert to type of Binary.

    For details of HttpPostedFileBase members, you may refer:
    http://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase_members.aspx

    Hope I understand correctly and hope it help.

    Thursday, September 17, 2009 9:39 PM
  • User-2089506584 posted

    I think your property data type does not match with type you want to assigned.

    What will my datatype be?


    Thursday, September 17, 2009 10:14 PM
  • User-1542018982 posted

    Hi,

    I think it should be datatype below in order for you to assign correct property.
    product.ImageMimeType: String Datatype
    product.ImageData: Binary Datatype

    Hope it help

    Thursday, September 17, 2009 10:22 PM
  • User-2089506584 posted

    I have now replace the datatypes as follows,

    ImageData - binary(50)
    ImageMimeType - nvarchar(MAX)

    But, I get this error "Value of type 'System.Data.Linq.Binary' cannot be converted to '1-dimensional array of Byte'." from my code below.

                product.ImageMimeType = image.ContentType
                product.ImageData = New Byte(image.ContentLength - 1) {}
                image.InputStream.Read(imageupload.ImageData, 0, image.ContentType) [:(]


    Wednesday, September 30, 2009 10:44 PM
  • Thursday, October 1, 2009 1:21 AM
  • User-2089506584 posted

    Hi kpyap,

    I'm getting this error "Input string was not in a correct format." [:(]

    image.InputStream.Read(imageupload.ImageData.ToArray, 0, image.ContentType)


    Thursday, October 1, 2009 3:09 AM
  • User-1542018982 posted

    Hi,

    I think you have assign wrong argument.

    Try this:
    image.InputStream.Read(imageupload.ImageData.ToArray, 0, imagefile.ContentLength)

    I think image should be HttpPostedFile. You can refer link before on how to use InputStream.Read
    http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream(VS.71).aspx

    For Stream.Read method, you may refer link below:
    http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx

    Hope it help.

    Thursday, October 1, 2009 3:39 AM
  • User-2089506584 posted

    Try this:
    image.InputStream.Read(imageupload.ImageData.ToArray, 0, imagefile.ContentLength)

    Hi kyyap,

    This time, I'm getting this error "No parameterless constructor defined for this object." [:(]

    Hope you'll stay with me. Thanks!


    Thursday, October 1, 2009 4:57 AM
  • User-1542018982 posted

    Hi,

    I think I miss the bracket after ToArray().

    image.InputStream.Read(imageupload.ImageData.ToArray(), 0, imagefile.ContentLength)

    Hope it help

    Thursday, October 1, 2009 6:19 AM