开发者

Exception: Object reference not set to an instance of an object

开发者 https://www.devze.com 2023-02-16 05:13 出处:网络
I have been using the code, object amountObject = MySqlDAL.ExecuteQuerySingle(query); if (amountObject.Equals(System.DBNull.Value))

I have been using the code,

  object amountObject = MySqlDAL.ExecuteQuerySingle(query);

        if (amountObject.Equals(System.DBNull.Value))
        {
            return amount;
        }

here in some point am getting an exception "Object reference not set to an instance of an object." from the sentence amountObject.Equals(System.DBNull.Value). Its working fine for some set of data.

What 开发者_开发问答may be the reason? Can any one please help me out?


Try checking the result for null as well..

if (amountObject ==null || amountObject.Equals(System.DBNull.Value))          
{             
     return amount;         
} 


Presumably MySqlDAL.ExecuteQuerySingle is returning null instead of System.DBNull.Value. It's hard to know whether that's a bug in your expectations or in ExecuteQuerySingle though.


Sometimes the query doesn't return a value, therefore amountObject is null.

Manually run the query with the parameters that make this fail, there's something wrong with your SQL.


Just change:
if (amountObject.Equals(System.DBNull.Value)) { return amount; }

to:
if (amountObject != null && amountObject.Equals(System.DBNull.Value)) { return amount; }


Have you checked if amountObject is null? It seems that the returned value by MySqlDAL.ExecuteQuerySingle return NULL.

0

精彩评论

暂无评论...
验证码 换一张
取 消