I want to return int and bool values to WP page from the database using WCF service. When those fields in the database are null
I'm getting an exception:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:someFuncResult . The InnerException message was 'ValueType 'System.Boolean' cannot be null. Please see InnerException for more details.
With int type I have the same problem.
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. After updating the reference it worked.
the database null is different then standard 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.