简体   繁体   English

无法返回null(System.Boolean不能为null)

[英]Can't return null (System.Boolean cannot be null)

I want to return int and bool values to WP page from the database using WCF service. 我想使用WCF服务从数据库将int和bool值返回到WP页。 When those fields in the database are null I'm getting an exception: 当数据库中的那些字段为null我得到一个例外:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:someFuncResult . 格式化程序尝试反序列化消息时引发异常:尝试反序列化参数http://tempuri.org/:someFuncResult时发生错误。 The InnerException message was 'ValueType 'System.Boolean' cannot be null. InnerException消息为'ValueType'System.Boolean'不能为null。 Please see InnerException for more details. 有关更多详细信息,请参见InnerException。

With int type I have the same problem. 与int类型,我有同样的问题。

Piece of my database looks like: 我的数据库片段如下所示:

[boolValue]    BIT    NULL,
[intValue]     INT    NULL,

My service implementation: 我的服务实现:

     public someClass someFunc(int check)
    {
return (from a in datacontext.someTable
                where a.number == check
                select new someClass ()
                {
                    intValue2 = (int?)a.intValue ,
                    boolValue2 = (bool?)a.boolValue,                
                }).Single();
    }

My service interface: 我的服务界面:

    [OperationContract]
   someClass someFunc(int check);

    [DataContract]
        public class someClass 
        {
            [DataMember]
            public int? intValue2 { get; set; }
            [DataMember]
            public bool? boolValue2 { get; set; }
        }

As Nullable<bool> field was introduced recently, reference on client side for my service wasn't updated. 由于Nullable<bool>字段是最近引入的,因此我的服务在客户端的引用未更新。 After updating the reference it worked. 更新参考后,它开始工作。

the database null is different then standard null 数据库null与标准null不同

use this function 使用这个功能

 static bool ConvertToBool(object Dnull)
        {
            try
            {
                DBNull a = (DBNull)Dnull;
                string u = a.ToString();

                if (u.Length > 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (InvalidCastException e)
            {
                return false;
            }

        }

and

bool Isnull = ConvertToBool(_sqldatareader["Name"]);

this work fine for in mysql. 这项工作在mysql中很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“System.Func`2[T,System.Boolean]”的表达式不能用于返回类型“System.Boolean” - Expression of type 'System.Func`2[T,System.Boolean]' cannot be used for return type 'System.Boolean' 无法在System.Boolean上测试 - Can't test on System.Boolean 无法将null值分配给类型为System.Boolean的成员,该类型是不可为空的值类型 - The null value cannot be assigned to a member with type System.Boolean which is a non-nullable value type “参数字典包含不可为空类型 &#39;System.Boolean&#39; 的参数 &#39;blueColor&#39; 的空条目” - 如何解决这个问题? - “The parameters dictionary contains a null entry for parameter 'blueColor' of non-nullable type 'System.Boolean'” - How to fix this? Visual Studio 2012无法将&#39;bool&#39;转换为&#39;System.Boolean&#39; - Visual Studio 2012 cannot convert from 'bool' to 'System.Boolean' ServiceStack / Funq无法解决System.Boolean异常 - ServiceStack/Funq cannot resolve System.Boolean exception 强制转换为值类型System.Boolean,因为物化值为null,结果类型的泛型参数必须使用可为空的类型 - The cast to value type System.Boolean failed because the materialized value is null, result type's generic parameter must use a nullable type 无法将“ y”的“ x”属性设置为“ System.Decimal”值。 您必须将此属性设置为“ System.Boolean”类型的非空值 - The 'x' property on 'y' could not be set to a 'System.Decimal' value. You must set this property to a non-null value of type 'System.Boolean' 亚超音速3-无法将类型为&#39;System.UInt64&#39;的对象转换为类型为&#39;System.Boolean&#39; - Subsonic 3 - Object of type 'System.UInt64' cannot be converted to type 'System.Boolean' 找不到针对System.ArgumentNullException的修复:值不能为null - Can't find a fix for System.ArgumentNullException: Value cannot be null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM