[英]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.