1

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?

5 Answers 5

2

Try checking the result for null as well..

if (amountObject ==null || amountObject.Equals(System.DBNull.Value))          
{             
     return amount;         
} 
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think this is a good idea, myself. It will work, but ExecuteQuerySingle should have one documented way of returning a null result... and that one way is what the client code should check for.
@Jon agreed, would be better taking a look inside ExecuteQuerySingle and fixing the problem at source.
Ok. Thanks for the info. Let me check.
2

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.

6 Comments

Yes Mr Jon its returning null value. So it cannot be checked in that way? It is that same as System.DBNull.Value right?
@Anjana: No, DBNull.Value is a non-null reference representing a null result from a database, but it's not the same as a null reference. You need to find out whether the method is meant to return a null reference or DBNull.Value, and act accordingly.
Mr jon. Now its working as i changed the checking criteria as ok if (amountObject ==null || amountObject.Equals(System.DBNull.Value)) { return amount; } Thanks for the info
@Anjana: While that will work, you shouldn't have to do that. Find out what it's meant to return, and only test for that.
@ Mr Jon actually the same piece of code has been used for two different scenarios.
|
0

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.

1 Comment

Yes. The problem was with the checking criteria. Now its worked.
0

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

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

Comments

0

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

1 Comment

Yes Mr Falcon its returning null value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.