Click here to Skip to main content
15,899,549 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

In my application i'm checking conditions with following way, but it seems not validating well(Always my catch block got exception).

C#
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.
Posted
Updated 27-Jun-18 0:03am
v2
Comments
Sushil Mate 5-Mar-12 2:11am    
Hope that table is not empty? can you put the value of that table?
J.Karthick 5-Mar-12 2:13am    
Yes....Thats what i'm checking this condition.Whether its empty or not
Ed Nutting 5-Mar-12 2:28am    
No he means does it actually have any rows in the table - if there are no rows then trying to get the value of VAT for row 0 isn't going to work and would throw the exception that you're getting. To test if the table is empty try doing: dtable.Rows.Count > 0 (I can't check this but it will be similar to this. It may be that Count is a method and so needs to be called as one.

Hope this helps,
Ed
J.Karthick 5-Mar-12 3:52am    
I have pasted my actual solution. Please check it out.

Thanks for your comments

You need to check if the statement returned a row at all:
C#
dreturns = 0;
if (dtable.Rows.Count > 0)
{
    if (dtable.Rows[0]["VAT"] != System.DBNull.Value)
        dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]);
}
 
Share this answer
 
Comments
ProEnggSoft 5-Mar-12 2:59am    
This is the correct solution. Comment above by Ed Nutting also points to the same thing. My 5.
J.Karthick 5-Mar-12 3:49am    
Nope....I don't accept this.

If my dtable is 'NULL' then how can i check row count ???

Even i tried this already.
C#
dreturns = 0;
if ( dtable != null && dtable.Rows.Count > 0 )
{
    if (dtable.Rows[0]["VAT"] != System.DBNull.Value)
        dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]);
}


is this solution i get over.


Anyways thanks for CP members for their support.
 
Share this answer
 
C#
if (!DBNull.Value.Equals(dtable.Rows[0]"VAT"])                        
                      dreturns = Convert.ToDecimal(dtable.Rows[0]["VAT"]);
                   else
                       dreturns = 0;
 
Share this answer
 
Comments
J.Karthick 5-Mar-12 5:32am    
yes...its also an another method to do it.

Thanks
Try comparing the value of the column to the DBNull.Value value to filter and manage null values in whatever way you see fit.

C#
foreach(DataRow row in dtable.Rows)
{
    object value = row["VAT"];
    if (value == DBNull.Value)
        // do something
    else
        // do something else
}
 
Share this answer
 
Comments
Richard Deeming 27-Jun-18 10:16am    
SIX YEARS too late. The question already has a couple of very similar solutions - except yours changes the behaviour of the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900