locked
convert byte[] to linq Binary in Silverlight? RRS feed

  • Question

  • I maintain pictures as SQL Server Image types in my database.  In my past web and Windows programs I could always convert a picture

    to byte[], put the byte[] into the image column and insert the record directly into the database.

    Nothing could have been more simple.  Now, enter Silverlight and LINQ...

    LINQ-To-SQL maps the SQL Server Image fields to "Binary" in my .dbml files so now I have to convert my byte[] to "Binary".  I receive

    a .jpg file that is dropped onto my silverlight form. If I were on the server instead of in my Silverlight code I could:

    System.Data.Linq.Binary binary = new System.Data.Linq.Binary(byteArray);

    But I don't have access to the System.Data.Linq.dll in Silverlight so I can't do that.

    So, how can I convert a byte[] to a "Binary" in Silverlight ??

    (I know that I can jimmy around the WCF data contract and include byte[] fields and then do the conversion on the server but it would

    be simpler and more maintainable to do the conversion right where I got the file)

    Thanks,
    Gary

     


    .net Developer
    Sunday, August 29, 2010 12:31 AM

Answers

  • Hi Gary,

     

    You can try to change the way LINQ-To-SQL mapping the SQL Server Image field from "Binary" to “byte[]”, then you do not need to do the conversion. On the dbml designer, click on the image column, and in its property window, change the “Type” property from “Binary (System.Data.Linq.Binary)” to “byte[] (System.Byte[])”.

     

    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 30, 2010 7:53 AM

All replies

  • Hi Gary,

     

    You can try to change the way LINQ-To-SQL mapping the SQL Server Image field from "Binary" to “byte[]”, then you do not need to do the conversion. On the dbml designer, click on the image column, and in its property window, change the “Type” property from “Binary (System.Data.Linq.Binary)” to “byte[] (System.Byte[])”.

     

    Best regards,

    Alex Liang

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 30, 2010 7:53 AM
  • Hi Gary, 

    I'm writing to follow up the post. Does the above suggestion work ?

    Please feel free to let me know if you need any help.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, September 2, 2010 9:53 AM
  • Hi Gary,

    I am marking my reply as answer. If you have any question, please feel free to let me know.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, September 13, 2010 1:20 AM
  • Hi Alex, changing System.Data.Linq.Binary to “byte[] System.Byte[] works fine when i fetch data from database, but when i need to do an insert or update operation, I use stored procedures, so i can not modify easily the data type to System.Byte[] because the sp is read only in the designer.

    Now I need to search several sp in the generated code via Show ALL button of visual studio to view the designer when the code is located.

    Then I can change manually the data type, but i have a lot of tables with System.Data.Linq.Binary to convert to byte[] System.Byte[].

    But when my team of developers change several tables and do a drag and drop to update the designer I have to do all this job again. It is a nightmare for me :-(

    Sunday, September 26, 2010 3:24 AM
  • More simple approach, use class.Binary = new Binary(byte[]) and you got it done.
    Thursday, April 14, 2011 9:37 PM