Decrease Font Size
Increase Font Size
   BLOG

Data is Null This method or property cannot be called on Null values

I was working on one of the VB.NET application that have been developed several years ago and I saw some declaration like Str7 = objDr.GetString(7).ToString, Str8 = objDr.GetString(8).ToString, Str9 = objDr.GetString(9).ToString and so on. It puzzled me, what will happen if the value in the column 7, 8, or 9 is null. Then, I went in to the SQL database, pull out the table and changed the value to null. And I received this error "

Data is Null. This method or property cannot be called on Null values.

" If you happen to come across this situation, below is the work around.

 

Solution:

If (Not objDr(8) Is DBNull.Value) Then
     Str8 = Trim(objDr.GetString(8).ToString)
End If