如何解决此错误对象无法从DBNull强制转换为其他类型。 [英] how to solve this error Object cannot be cast from DBNull to other types.
问题描述
如何解决此错误使用orcle sqldeveloper作为后端,无法将对象从DBNull强制转换为其他类型的iam。
com_id = Convert.ToInt32(ds.Tables [0] .Rows [0] [COMPANYID]);
听到我的COMPANYID coloumn我有空白数据。所以我得到了这个错误。如何解决它。
请帮助我
至少有几种可能的方式:
1) 使用可空类型(C#编程指南) [ ^ ]
// 声明可为空的整数值
int ? myValue = ds.Tables [ 0 ]。行[ 0 ] [ COMPANYID];
if (myValue!= null )
{
// 继续
}
2)使用lambda表达式 [ ^ ] DbNull.Value [ ^ ]:
int result = ds.Tables [ 0 ]。行[ 0 ] [ COMPANYID] == DbNull.Value? 0 :Convert.ToInt32(ds.Tables [ 0 ]。行[ 0 ] [ COMPANYID]);
3)使用 Int32.TryParse方法 [ ^ ]:
int result = 0 跨度>;
if ( Int32 .TryParse(ds.Tables [ 0 ]。行[ 0 ] [ COMPANYID],结果))
{
// 结果已成功转换为整数值
}
试试!
< blockquote>检查出来 -
com_id = Convert.IsDBNull(ds.Tables [ 0 ]。行[ 0 ] [ COMPANYID] )? 0 :Convert.ToInt32(ds.Tables [ 0 ]。行[ 0 ] [ COMPANYID]);
这里如果DBNull,com_id可以分配一些默认值,可以在以后处理。
how to solve this error Object cannot be cast from DBNull to other type iam using orcle sqldeveloper as backend.
com_id=Convert.ToInt32(ds.Tables[0].Rows[0]["COMPANYID"]);
hear in my COMPANYID coloumn i have blank data. so i got this error. how to resolve it.
help me please
There's at least few possible ways:
1) Using Nullable Types (C# Programming Guide)[^]
//declare nullable integer value int? myValue = ds.Tables[0].Rows[0]["COMPANYID"]; if (myValue!=null) { //proceed }
2) Using lambda expressions[^] with DbNull.Value[^]:
int result = ds.Tables[0].Rows[0]["COMPANYID"] == DbNull.Value ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["COMPANYID"]);
3) Using Int32.TryParse method[^]:
int result = 0; if(Int32.TryParse(ds.Tables[0].Rows[0]["COMPANYID"], result)) { //result has been succesfully converted into integer value }
Try!
Check this out-
com_id = Convert.IsDBNull(ds.Tables[0].Rows[0]["COMPANYID"]) ? 0 : Convert.ToInt32(ds.Tables[0].Rows[0]["COMPANYID"]);
here if DBNull, com_id can be assigned with some default value which could be handled later.
这篇关于如何解决此错误对象无法从DBNull强制转换为其他类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!