Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In the following code I get the exception 'Data is Null. This method or property cannot be called on Null values.' at line if(reader1...)

SQL
while (reader1.Read())
if(reader1.GetDateTime(9) !=null)
Posted
Comments
Sergey Alexandrovich Kryukov 22-May-14 2:17am    
I thought that after asking 251 questions one could learn how to ask them. :-)
Are you even trying to learn anything from the answers and comments you receive? Still the same story: no debugging, not relevant information...
—SA

Hi,
I think your reader1 doesn't have a value. Please check if it has. Then come back.

Or, there's nothing to read on reader1.
Thanks.
 
Share this answer
 
v2
Try this

int x = reader.GetOrdinal("datefield");

if(!reader.IsDBNull(x))
dateValue = reader.GetDateTime(x);
 
Share this answer
 
Here the exception comes only if the reader1 doesn't contain any value . But you are checking this while while loop . So modify your code as below :
VB
while (reader1.Read())
{
if(reader1.GetDateTime(9) !=null)

//your logic here
}
 
Share this answer
 
Hi,
i think your Reader doesnt has any value so your reader.read () is always false , you should check your ado.net commands .

please chechk wether reader.hasrows is true or false and reader.fieldscount how much it shows
then you know your issue. :)
 
Share this answer
 

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