Reputation: 189
I am running an SQL Query with data readers in vb.net
reader3 = myCommand3.ExecuteReader
reader3.Read()
If reader3.HasRows Then
FromToDates = reader3.GetString(3)
End If
There are no rows being returned in this particular query, however even though the if reader3.HasRows
is being used its still showing an error saying:
Additional information: Data is Null. This method or property cannot be called on Null values.
this is when trying to set the FromToDates
variable but there are no rows being returned so it should not even reach this part
Upvotes: 0
Views: 6810
Reputation: 73
It seems that even though the row is empty (all DBNull, perhaps), there IS a row object returning from the query. There are several different solutions presented in this thread that all take differing approaches to catching the empty row before it errors!
http://www.vbforums.com/showthread.php?555183-datareader-and-empty-records
Upvotes: 0
Reputation: 460048
I doubt that there are no rows, i assume that there is at least one row where the value is NULL
. You could check it with the IsDBNull
-method:
If reader3.HasRows AndAlso Not reader3.IsDbNull(3) Then
FromToDates = reader3.GetString(3)
End If
However, the variable name suggests that it's a date, but you are storing it as string. Use the correct type which is date
or datetime
. If it's actually a datetime
use:
Dim FromToDates As Date
If reader3.HasRows AndAlso Not reader3.IsDbNull(3) Then
FromToDates = reader3.GetDateTime(3)
End If
Upvotes: 2
Reputation: 174
if you are using Reader.read() then i don't think so you need reader,hasrow(). you can do somthing like this
reader3 = myCommand3.ExecuteReader
If reader3.Read()Then
FromToDates = reader3.GetString(3)
End If
and related to your error its look like you are getting NULL value from database and in vb you can not read NULL, i will suggest to use ISNULL(Column_NAME,'') function in your SQL Script.
Upvotes: 0