Unable to cast object of type 'System.DBNull' to type 'System.Byte[]

10 posts, 1 answers
  1. Thomas
    Thomas avatar
    6 posts
    Member since:
    Dec 2008

    Posted 27 Aug 2009 Link to this post

    I am using RadBinaryImage control with SqlDataSource. If the VarBinary field contains a NULL value I am getting this error:  “Unable to cast object of type 'System.DBNull' to type 'System.Byte[]' “.

    My understanding is that a NULL field should be cast to  a null or empty byte[].

    Can someone please show me how check if the supplied value is of type DBNull and how to change it to appropriate empty byte array or null in C#?


    Thanks,
    Thomas
  2. BaiH
    BaiH avatar
    83 posts
    Member since:
    Aug 2008

    Posted 28 Aug 2009 Link to this post

    Hey Thomas, I think you have missed the first thread in this forum ;) Here is a link.

    --BH
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Thomas
    Thomas avatar
    6 posts
    Member since:
    Dec 2008

    Posted 28 Aug 2009 Link to this post

    I have read the first thread and I understand that the field should be cast to null or empty byte[]. My question was: how to do it.  I would appreciate if someone would help me with an example in C# .

    Thanks,
    Thomas
  5. Answer
    Growls
    Growls avatar
    15 posts
    Member since:
    Apr 2009

    Posted 31 Aug 2009 Link to this post

    to do it in markup:
     <telerik:RadBinaryImage runat="server" ID="rbi" DataValue='<%# ((System.Data.Linq.Binary) Eval("imageFull")).ToArray() %>' /> 

    or do it dynamically:

    var product = (from p in products 
                   select p).First();//.First() = LIMIT 1 in sql, which in turn makes the product variable singular and usable like this: 
    rsi.DataValue = product.imageFull.ToArray(); 



  6. Thomas
    Thomas avatar
    6 posts
    Member since:
    Dec 2008

    Posted 06 Sep 2009 Link to this post

    Thank you Craig. This is what I was looking for.
    Thomas
  7. Bodie Sullivan
    Bodie Sullivan avatar
    2 posts
    Member since:
    Aug 2009

    Posted 17 Sep 2009 Link to this post

    I am having the same issue but my project is in VB using .Net 2.0... What would the syntax be for VB with out using LINQ to convert a null value in the markup?
  8. Derek
    Derek avatar
    118 posts
    Member since:
    Oct 2008

    Posted 15 Nov 2009 Link to this post

    I'd like to see the solution in VB (and without using LINQ) as well. Anyone?
  9. Ilya Tryapitsin
    Ilya Tryapitsin avatar
    1 posts
    Member since:
    Dec 2009

    Posted 25 Dec 2009 Link to this post

    For VB without LINQ you can use IIf function.
    For example:
     < asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">     
        <ItemTemplate> 
        ...   
            <telerik:RadBinaryImage ID="RadBinaryImage1" runat="server" DataValue='<%#IIf(Typeof(Eval("photo")) is DBNull, Nothing, Eval("photo"))%>' /> 
        ...   
         </ItemTemplate> 
     
     
     

     

  10. Richard
    Richard avatar
    1 posts
    Member since:
    Mar 2012

    Posted 23 Mar 2012 Link to this post

    I think you should check the value containing DBNUll or not , You can check DBNull value using System.DBNull.Value , find the full source code to check DBNull value here :

    http://net-informations.com/csprj/ado.net/cs-dbnull.htm

    algr.

  11. syeada sanjida
    syeada sanjida avatar
    1 posts
    Member since:
    Jan 2015

    Posted 29 Jan 2015 in reply to Thomas Link to this post

     when i can editing this message show .why this problem show unable to cast object of type system.dbnull to type 'system.byte[]
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017