DBNULL无法验证 [英] DBNULL not Validate well
本文介绍了DBNULL无法验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我正在按照以下方式检查条件,但似乎没有很好地验证(总是我的catch块异常)。 />
Hi,
In my application i'm checking conditions with following way, but it seems not validating well(Always my catch block got exception).
if (dtable.Rows[0]["VAT"] != System.DBNull.Value) //Exception here while it returns DBNULL
dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]);
else
dreturns = 0;
任何帮助都将不胜感激。
捕获错误是:
Any help would be appreciated.
Catched Error is :
Error : System.NullReferenceException: Object reference not set to an instance of an object.
推荐答案
您需要检查语句是否完全返回行:
You need to check if the statement returned a row at all:
dreturns = 0;
if (dtable.Rows.Count > 0)
{
if (dtable.Rows[0]["VAT"] != System.DBNull.Value)
dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]);
}
dreturns = 0;
if ( dtable != null && dtable.Rows.Count > 0 )
{
if (dtable.Rows[0]["VAT"] != System.DBNull.Value)
dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]);
}
这是我解决的解决方案。
无论如何,感谢CP成员的支持。
is this solution i get over.
Anyways thanks for CP members for their support.
if (!DBNull.Value.Equals(dtable.Rows[0]"VAT"])
dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]);
else
dreturns = 0;
这篇关于DBNULL无法验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文