Reputation: 3358
I'm getting some data from Stored Procedure to DataSet and then copying that data to a List. There are some NULL values in data and for that I'm checking with DBNULL.Value. But whenever it comes to a NULL value, it gives me error
ERROR : Specified cast is not valid.
This is how I'm copying data from DataSet to List.
AreaId = dsTable.SearchedTable[I]["AreaId"] == DBNULL.Value ? 0 : (long)dsTable.SearchedTable[I]["AreaId"];
Sometimes it works fine but sometimes it gives me error.
And when I change this DBNULL.Value to IsAreaIdNull()
. It works fine.
AreaId = dsTable.SearchedTable[I].IsAreaIdNull() ? 0 : (long)dsTable.SearchedTable[I]["AreaId"];
I'm confused that what's wrong with DBNull.Value because it is working perfectly fine in other procedures. Even in same procedure for other values. M i missing something in it??
Any kind of help will be appreciated.
Upvotes: 1
Views: 66
Reputation: 26886
It looks like you have a typo: assignment instead of equality comparison here:
dsTable.SearchedTable[I]["AreaId"] = DBNULL.Value ? 0 :
(long)dsTable.SearchedTable[I]["AreaId"];
It should be
dsTable.SearchedTable[I]["AreaId"] == DBNULL.Value ? 0 :
(long)dsTable.SearchedTable[I]["AreaId"];
Upvotes: 2