ASP.NET学习社区

首页 » ASP.NET学习区 » 自由问答 » Object cannot be cast from DBNull to other types.高手求救。
77631527 - 2008-5-26 17:17:00
Object cannot be cast from DBNull to other types.
whgfu - 2008-5-26 17:27:00
怎么出现这个问题的,你怎么不说啊。。汗,这怎么帮你、。

看看这里能帮助你不

避免Object cannot be cast from DBNull to other types. 错误
if(sRows[0]["Cycle"].GetType() != Type.GetType("System.DBNull"))//check if DBNull
                            outCycle = Convert.ToInt32(sRows[0]["Cycle"]);






Should like this:

public static int? GetInt32Nullable(System.Data.IDataReader dataReader, int i)
{
if(dataReader.IsDBNull(i))
{
return null;
}
return dataReader.GetInt32(i);
}
追づ风 - 2008-5-26 19:21:00
有问题先找baidu
实在解决不了再发帖-_-
aspx1 - 2008-5-27 7:01:00
你读取的数据库字段是DBNull ,即该记录的该字段为空,而你又想将此dbnull空字段转化为其他数据类型(比如int,datetime等),当然无法转换了,就出现了这个错误。
你在数据转换之前应该先判断一下该字段是否为空,如果非空再执行转换。
1
查看完整版本: Object cannot be cast from DBNull to other types.高手求救。