This is a migrated thread and some comments may be shown as answers.

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

9 Answers 2101 Views
BinaryImage
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 27 Aug 2009, 07:20 PM

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

9 Answers, 1 is accepted

Sort by
0
BaiH
Top achievements
Rank 1
answered on 28 Aug 2009, 06:29 AM
Hey Thomas, I think you have missed the first thread in this forum ;) Here is a link.

--BH
0
Thomas
Top achievements
Rank 1
answered on 29 Aug 2009, 03:44 AM
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
0
Accepted
Growls
Top achievements
Rank 1
answered on 31 Aug 2009, 05:11 AM
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(); 



0
Thomas
Top achievements
Rank 1
answered on 07 Sep 2009, 02:51 AM
Thank you Craig. This is what I was looking for.
Thomas
0
Bodie Sullivan
Top achievements
Rank 2
answered on 17 Sep 2009, 08:15 PM
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?
0
Derek
Top achievements
Rank 1
answered on 15 Nov 2009, 11:48 PM
I'd like to see the solution in VB (and without using LINQ) as well. Anyone?
0
Ilya Tryapitsin
Top achievements
Rank 1
answered on 25 Dec 2009, 09:19 AM
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> 
 
 
 

 

0
Richard
Top achievements
Rank 1
answered on 23 Mar 2012, 07:42 AM
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.

0
syeada sanjida
Top achievements
Rank 1
answered on 29 Jan 2015, 12:05 PM
 when i can editing this message show .why this problem show unable to cast object of type system.dbnull to type 'system.byte[]
Tags
BinaryImage
Asked by
Thomas
Top achievements
Rank 1
Answers by
BaiH
Top achievements
Rank 1
Thomas
Top achievements
Rank 1
Growls
Top achievements
Rank 1
Bodie Sullivan
Top achievements
Rank 2
Derek
Top achievements
Rank 1
Ilya Tryapitsin
Top achievements
Rank 1
Richard
Top achievements
Rank 1
syeada sanjida
Top achievements
Rank 1
Share this question
or