Last post Feb 09, 2010 09:07 AM by atconway
Jan 28, 2010 02:05 AM|JohnnyLaRue|LINK
Hi. Does anyone know how to test vb code for null values. I tried this but it does not seem to work:
null value vb code test
Jan 28, 2010 03:15 AM|vik20000in|LINK
dbnull and null are differnet thing. DBNUll is null value of database and null value in memory.
So try checking like this
if irow("Address") = nothing begin
Jan 28, 2010 09:46 AM|JohnnyLaRue|LINK
Thanks, but when I try for nothing I get this error:
Operator '=' is not defined for type 'DBNull' and 'Nothing'
query test for null value
Feb 01, 2010 05:29 PM|chickflickssuck|LINK
Assuming the value you are checking, is a string, I have a function like the following:
'Check if the value is DbNull, Nothing, or an Empty String.
'If it is, or an error is thrown, return false. else return true.
Private Function hasValue(ByVal pStr as String) As Boolean
If IsDbNull(pStr) Then
Else If pStr = Nothing Then
Else If pStr.replace(" ","") = "" Then
Catch ex as Exception
Feb 02, 2010 07:06 PM|asgradl84|LINK
Feb 09, 2010 09:07 AM|atconway|LINK
When checking a DataRow object for value, and that the actual column name exists, so you do not get an error trying to access a column that does not exists, use the following code:
'Declare a variable to hold the address (or a class property, etc.)
Dim MyAddress As String = String.Empty
'Attempt to populate the variable declared above
'If the 'Address' value in the DataRow does not exist, then the variable is just not populated.
'The 'AndAlso' operator will not test foe a value if the 1st expression is not 'True'
'This prevents throwing any errors for acessing values of columns that do not exist:
If irow.Table.Columns.Contains("Address") AndAlso Not IsDBNull(irow("Address")) Then MyAddress = irow("Address").ToString.Trim()
Hope this helps!