none
Unable to cast object of type 'System.Data.Linq.Binary' to type 'System.Byte[]', ASP.NET and LINQ to SQL

    Question

  • Hello. I'm trying to display images in a gridview, using Linq to SQL connection. Getting error "Unable to cast object of type 'System.Data.Linq.Binary' to type 'System.Byte[]'"

    The code is

    <asp:TemplateField HeaderText="Photo1">
                                    <ItemTemplate>
                                        <asp:Image ID="Photo1" runat="server" Height="100px" Width="100px" ImageUrl='<%#"data:Image/png;base64," + Convert.ToBase64String(((Byte[])Eval("Photo1")).ToArray()) %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
    What is the proper way to convert Binary to Byte and display it in the GRIDVIEW? Thanks!

    Alla Sanders


    • Edited by Alla2552 Tuesday, January 5, 2016 2:46 PM double lines
    Tuesday, January 5, 2016 2:46 PM

Answers

  • Found a solution. Was missing reference in web.config

    <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

    And the conversion part is

    Convert.ToBase64String(((System.Data.Linq.Binary)Eval("Photo1")).ToArray())
    


    Alla Sanders

    • Marked as answer by Alla2552 Tuesday, January 5, 2016 9:32 PM
    Tuesday, January 5, 2016 9:31 PM

All replies

  • Hello Alla,

    Your code looks weird, some try with Apples and Irons to do something. 

    However, you can better ask this in the Microsoft ASP.Net forums. 

    In the current forums you will need at least code behind.

    http://www.asp.net/


    Success
    Cor

    Tuesday, January 5, 2016 3:34 PM
  • Found a solution. Was missing reference in web.config

    <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

    And the conversion part is

    Convert.ToBase64String(((System.Data.Linq.Binary)Eval("Photo1")).ToArray())
    


    Alla Sanders

    • Marked as answer by Alla2552 Tuesday, January 5, 2016 9:32 PM
    Tuesday, January 5, 2016 9:31 PM