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
0
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
Thanks,
Thomas
0
Accepted

Growls
Top achievements
Rank 1
answered on 31 Aug 2009, 05:11 AM
to do it in markup:
or do it dynamically:
<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
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:
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.
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[]