Using LINQ datasource and FormView

12 posts, 0 answers
  1. Perry Robertson
    Perry Robertson avatar
    16 posts
    Member since:
    Nov 2004

    Posted 09 Jul 2009 Link to this post

    First I must say, thanks for this new addition, defintely a nice to have feature.

    However I am having some issues with it when not used inside a RadGrid as a BinaryImageColumn.

    I took my existing app that used pictures on the file system and changed it to use images in the database using BinaryImage.  My datasources are all LINQ Data Sources.  My SQL 2000 table has a photo column of type 'Image'.  I am using a FormView that gets it data from my LINQ data source. 
    Looking at your example you use a SQL data source and I would guess that it returns the Image column as a byte[] but when using LINQ my image column comes back as System.Data.Linq.Binary.  This causes a 'Specified cast is not valid' exception.
    My first work around was to change my Linq definition of my image column to type Byte[] and all works well. Except in another part of my app I use a RadGrid and the BinaryImageColumn it fails to work there.  I believe its the opposite issue that it's expecting the other type.
    I would prefer to leave my LINQ definition to have the default Image column as a System.Data.Linq.Binary and deal with the casting issue.

    Can someone tell me the syntax of how to cast between Byte[] and System.Data.Linq.Binary in the aspx page or do I have to do it in code behind?

    Thanks,

    Perry
  2. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 10 Jul 2009 Link to this post

    Hi Perry,

    You can call ToArray method of the Binary instance similar to the following:

     <telerik:RadBinaryImage runat="server" ID="radBinaryImage"   
            DataValue='<%# ((Binary) Eval("LargePhoto")).ToArray() %>' /> 


    Best wishes,
    Rosen
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Perry Robertson
    Perry Robertson avatar
    16 posts
    Member since:
    Nov 2004

    Posted 10 Jul 2009 Link to this post

    Thanks for the reply.  However I get a compile error when I try using (Binary) cast.

    I tried
    <telerik:RadBinaryImage runat="server" ID="RadBinaryImage1" DataValue='<%# ((Binary) Eval("member_photo")).ToArray() %>' />
    but I had to add a reference to the top of the aspx <%@ Import Namespace="System.Data.Linq"%> but then I get the compiler error below.  I also tried casting directly as (System.Data.Linq.Binary) and get same result.
    Compiler Error Message: CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)

    My project is referencing System.Data and System.Data.Linq.  I also tried using a code behind method that returns the binary type.
    DataValue='<%# showPhoto(Eval("member_photo")) %>' in which produces this error...
    CS0012: The type 'System.Data.Linq.Binary' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
    I validated that this assembly is in my GAC.  I use LINQ in many other places of my project so it isn't making sense why using (Binary) is causing me these compiler issues. 

    Any ideas appreciated.

    Thanks,
    Perry
  5. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 14 Jul 2009 Link to this post

    Hi Perry,

    I'm afraid that I'm not sure what may cause such behavior from the given details. I have attached a small test app, please take a look maybe I'm missing something obvious.

    All the best,
    Rosen
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Perry Robertson
    Perry Robertson avatar
    16 posts
    Member since:
    Nov 2004

    Posted 14 Jul 2009 Link to this post

    Hi Rosen,

    The problem I'm having now appears to not have anything to do with LINQ or the BinaryImage but rather my project type being a web app vs a web site.

    1) I create a blank Web Site (.net 3.5), added the System.Data.Linq reference and Import on the default page, added <%# ((Binary)Eval("test")).ToArray() %> and it works just fine, no compile errors or run-time errors and Visual Studio syntax hilighting recognizes (Binary).

    2) I create a blank Web Application (.net 3.5), did the exact steps as I did on the Web Site, and I get

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
    Compiler Error Message: CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)
    Source Error:
    Line 1:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>
    Line 2: <%@ Import Namespace="System.Data.Linq"%>Line 3:  
    Line 4:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    So this seems totally bizarre and has nothing to do with Telerik as I didn't even use any telerik references or code in these tests.  If you any suggestions I can try I'd greatly appreciate it.  I'm running Vista 64 Enterprise and using Visual Studio 2008 with all the latest updates on .net, visual studio, and my OS.

    Regards,

    Perry

  7. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 15 Jul 2009 Link to this post

    Hi Perry,

    I think this is a VS2008 issue as it seems that the namespace cannot be resolved when inside design-time's source view. In order to workaround this please add the following declaration in your webconfig assemblies section:

    <assemblies>                                    
        <!--other assemblies declaration-->                      
        <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>  
    </assemblies> 

    Best wishes,
    Rosen
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  8. Perry Robertson
    Perry Robertson avatar
    16 posts
    Member since:
    Nov 2004

    Posted 15 Jul 2009 Link to this post

    Thank-you.  That worked in both my sample application and my original project I tried implementing RadBinaryImage into, odd that that assembly is only being recognized by web sites and not web apps even with a reference to it.

    Anyhow, moving on with my original issue of trying to get radbinaryimage working...

    My db table has multiple entries and an 'Image' column that has an uploaded image in it.  When I use RadBinaryImage and set the datavalue ='<%# ((Binary)Eval("member_photo")).ToArray() %>' it works just fine.  I see my image when viewing that record.  The problem now is when I try to use this in a RadGrid.  the GridBinaryImageColumn is only producing a dead image placeholder icon, but when I add a GridTemplateColumn right after with a RadBinaryImage I see the image just fine in that column.

    <telerik:GridBinaryImageColumn DataField="member_photo" HeaderText="Image" UniqueName="Upload" ImageAlign="NotSet" 
            ImageHeight="80px" ImageWidth="80px" ResizeMode="Fit" DataAlternateTextField="member_name" 
            DataAlternateTextFormatString="Image of {0}">  
            <HeaderStyle Width="10%" />   
    </telerik:GridBinaryImageColumn>  
     
    <telerik:GridTemplateColumn AllowFiltering="false">  
     <ItemTemplate>  
                <telerik:RadBinaryImage runat="server" ID="RadBinaryImage1" DataValue='<%# ((Binary) Eval("member_photo")).ToArray() %>'  
                    AutoAdjustImageControlSize="false" Width="64" ToolTip='<%#Eval("member_name""Photo of {0}") %>'  
                    AlternateText='<%#Eval("member_name""Photo of {0}") %>' />  
     </ItemTemplate>  
    </telerik:GridTemplateColumn>   
     

    This produces a dead image and a valid image.  To explain further, my LINQ .dbml file has member_photo defined as type 'Image' because that is what my database table column datatype is.  My image in my database was inserted using "m.member_photo = new System.Data.Linq.Binary(img);"  img is my inputstream from the uploader.  When I use RadBinaryImage it's DataValue must be of byte[] type so we cast it using Binary of the ToArray().  But if GridBinaryImageColumn also must be a byte[] but I'm passing it a Linq.Binary.  And when I try to add the DataType property there is no 'Image' type to choose and I tried System.Byte, System.String, System.SByte, Image, System.Data.Linq.Binary, Binary, etc. and it doesn't work.

    Is this correct?  What should be the appropriate?  Is my error in using a database 'Image' column type?

    Just to add I just took the sample project and added a radgrid connected to my db and used a SQLDataSource instead of a Linq datasource and I get a Parameter is not valid exception for the radbinaryimage.  It appears using 'Image' db type isn't valid.  You sample showed that your column type is "VarBinary(MAX)" and not "Image".  If this is a Telerik design I would suggest that your RadBinaryImage can handle Image columns.  Any clarification much appreciated.

    Regards,
    Perry



  9. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 20 Jul 2009 Link to this post

    Hello Perry,

    RadBInaryImage, as RadGrid's BinaryImageColumn should show image data as long as the binary data itself is a valid image. Usually the error you have described is caused by invalid image data or image data which has a custom header applied. Can you please verify that this is not the case?

    In order to feed the GridBinaryImageColumn with data of type System.Data.Linq.Binary you may consider using a projection in order to convert the data before feeding it to the RadGrid.

    All the best,
    Rosen
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  10. Perry Robertson
    Perry Robertson avatar
    16 posts
    Member since:
    Nov 2004

    Posted 20 Jul 2009 Link to this post

    Hi Rosen,

    For testing, I used Microsoft Paint and drew a 200x200 image and saved it as a jpg so that we know there isn't something special with the file.  I uploaded the picture into my database using my web app uploader.  When using RadBinaryImage it shows the image fine.  When using RadBinaryImageColumn I get a dead image.  Is RadBinaryImage and ...Column both expect a Byte[] ?  Since I have to use .ToArray() when using RadBinaryImage how would I do it for ...Column when you only get to pick the field?

    Okay, I just did a few more tests... I moved the database from MSDE 2000 to Sql 2008 Express and no difference, what I expected.
    My next test....I made another field called member_photo2 and made it VarBinary(MAX) as apposed to Image and in my LINQ dbml the type is Byte[] as apposed to System.Data.Linq.Binary.  I modified my code to reference the member_photo2 and got rid of any binary casting and .toarray() that I had.  I uploaded the same image into the new field and it works 100% when viewing through RadBinaryImage and RadBinaryImageColumn.

    So to me this proves that RadBinaryImageColumn can't take a db column of type Image and Linq data type System.Data.Linq.Binary because it's expecting a Byte[].  I would of assumed that a RadBinaryImage can handle a Server Data Type of "Image" well before "VarBinary()".  I would recommend if it's possible if RadBinaryImage internally can differentiate if that data is a varbinary or an image type and do the appropriate steps.

    Let me know if you need more information.

    Best Regards,

    Perry
  11. BaiH
    BaiH avatar
    83 posts
    Member since:
    Aug 2008

    Posted 23 Jul 2009 Link to this post

    Man, As I see it RadBinaryImage expects only byte array, therefore you cannot pass different types to it as the property is of type byte array and will blow up . You may check this thread for similar discussion on data types passed into the binaryImage. You should cast it to the correct type before assigning it to the control.

    --BH
  12. Perry Robertson
    Perry Robertson avatar
    16 posts
    Member since:
    Nov 2004

    Posted 23 Jul 2009 Link to this post

    Thanks.  I understand your point.  My question then is if my database is using a "Image" db type and not "VarBinary(MAX)" and my LINQ dbml has the column defined as "Image" how do I make it work with RadBinaryImageColumn.  What we've already discovered above is that RadBinaryImage can be casted and use ToArray() to get a byte[] but with RadBinaryImageColumn you can only provide the column from the datasource and when it's of Image type it doesn't work, for obvious reasons.  What would I do either in t-sql or in C# code behind and is there an event I can override to intercept to achieve my conversion of my Image to byte[] for RadBinaryImageColumn?

    Regards,
    Perry
  13. BaiH
    BaiH avatar
    83 posts
    Member since:
    Aug 2008

    Posted 28 Jul 2009 Link to this post

    Sorry for not replying sooner but I was on a small vacation. To your question as the Telerik dude said you should use a projection to convert your entity object to a temporary one that have a property which returns the image's data as a byte array.

    --BH


Back to Top
UI for ASP.NET Ajax is Ready for VS 2017