Attachments not storing properly in database

Thread is closed for posting
2 posts, 0 answers
  1. Baal
    Baal avatar
    47 posts
    Member since:
    Mar 2008

    Posted 20 May 2010 Link to this post

    I have radUpload control in my page from which users can only upload images.

    <telerik:RadUpload ID="ruImage" runat="server" InitialFileInputsCount="1" ControlObjectsVisibility="None" AllowedMimeTypes="image/bmp,image/gif,image/jpeg,image/pjpeg,image/tiff,image/x-png" />  

    I am using the following code in my codebehind to upload the images to the database.
    if (ruImage.UploadedFiles.Count > 0) 
        AppImages oImage = null
        foreach (UploadedFile theFile in ruImage.UploadedFiles) 
            string fileName = theFile.FileName; 
            fileName = fileName.Substring(fileName.LastIndexOf(@"\")).Replace(@"\"""); 
            byte[] myBytes = new byte[theFile.ContentLength]; 
            theFile.InputStream.Read(myBytes, 0, theFile.ContentLength); 
            oImage = new AppImages(); 
            oImage.AppId = appId; 
            oImage.FileName = fileName; 
            oImage.FileSize = theFile.ContentLength; 
            oImage.FileType = theFile.ContentType; 
            oImage.Attachment = myBytes; 
            oImage.LastUpdatedBy = UserName; 
            oImage = null

    The column Attachment in the AppImages table is of type IMAGE. When I debug the code, everything is good as it should be except the content of myBytes. every single element of the array will have 0 as its value. Even when I check the database (I'm using SQL Server 2008), the content of the Attachment column will be 0x000...

    Now when I try to load the images to the grid (I have GridBinaryImageColumn), I get the following error from the IE8 debugger:
    Error: Sys.WebForms.PageRequestManagerServerErrorException: The provided binary data may not be valid image or may contains unknown header

    When I force the page to load, I get the following error:
    Parameter is not valid.  
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.  
    Exception Details: System.ArgumentException: Parameter is not valid. 
    Source Error:  
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.   
    Stack Trace:  
    [ArgumentException: Parameter is not valid.] 
       System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) +430 
       System.Drawing.Image.FromStream(Stream stream) +14 
       Telerik.Web.UI.ImageFilterHelper.CreateImgFromBytes(Byte[] image) +77 
    [ArgumentException: The provided binary data may not be valid image or may contains unknown header] 
       Telerik.Web.UI.ImageFilterHelper.CreateImgFromBytes(Byte[] image) +219 
       Telerik.Web.UI.RadBinaryImage.AutoAdjustImageElementSize(Byte[] dataValue) +26 
       Telerik.Web.UI.RadBinaryImage.ProcessImageData() +106 
       Telerik.Web.UI.RadBinaryImage.OnPreRender(EventArgs e) +27 
       System.Web.UI.Control.PreRenderRecursiveInternal() +108 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Control.PreRenderRecursiveInternal() +224 
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394 
    Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927  

    I've searched the net for this error but coulnd't find any help. Have you guys seen anything like this?


  2. Baal
    Baal avatar
    47 posts
    Member since:
    Mar 2008

    Posted 21 May 2010 Link to this post

    The problem while uploading the file was that somehow the position of the InputStream wasn't at 0. So I added theFile.InputStream.Position = 0 before populating the byte array with the data from the InputStream and it worked fine.
Back to Top