I have this:
If String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
Now, when editTransactionRow.pay_id
is Null Visual Basic throws an exception. Is there something wrong with this code?
If Short.TryParse(editTransactionRow.pay_id, New Short) Then editTransactionRow.pay_id.ToString()
The equivalent of null
in VB is Nothing
so your check wants to be:
If editTransactionRow.pay_id IsNot Nothing Then
stTransactionPaymentID = editTransactionRow.pay_id
End If
Or possibly, if you are actually wanting to check for a SQL null value:
If editTransactionRow.pay_id <> DbNull.Value Then
...
End If
This is the exact answer. Try this code:
If String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
editTransactionRow.pay_id is Null so in fact you are doing: null.ToString() and it cannot be executed. You need to check editTransactionRow.pay_id and not editTransactionRow.pay_id.ToString();
You code should be (IF pay_id is a string):
If String.IsNullOrEmpty(editTransactionRow.pay_id) = False Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
If pay_id is an Integer than you can just check if it's null normally without String... Edit to show you if it's not a String:
If editTransactionRow.pay_id IsNot Nothing Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
If it's from a database you can use IsDBNull but if not, do not use it.
If Short.TryParse(editTransactionRow.pay_id, New Short) Then editTransactionRow.pay_id.ToString()
If Not editTransactionRow.pay_id AndAlso String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
editTransactionRow.pay_id is Null so in fact you are doing: null.ToString() and it cannot be executed. You need to check editTransactionRow.pay_id and not editTransactionRow.pay_id.ToString();
You code should be (IF pay_id is a string):
If String.IsNullOrEmpty(editTransactionRow.pay_id) = False Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
If pay_id is an Integer than you can just check if it's null normally without String... Edit to show you if it's not a String:
If editTransactionRow.pay_id IsNot Nothing Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
If it's from a database you can use IsDBNull but if not, do not use it.
You can also use the IsDBNull function:
If Not IsDBNull(editTransactionRow.pay_id) Then
...
This is the exact answer. Try this code:
If String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
I find the safest way is
If Not editTransactionRow.pay_id Is Nothing
It might read terribly, but the ISIL is actually very different from IsNot Nothing, and it doesn't try and evaluate the expression, which could give a null reference exception.
You have to check to ensure editTransactionRow is not null and pay_id is not null.
You have to check to ensure editTransactionRow is not null and pay_id is not null.
The equivalent of null
in VB is Nothing
so your check wants to be:
If editTransactionRow.pay_id IsNot Nothing Then
stTransactionPaymentID = editTransactionRow.pay_id
End If
Or possibly, if you are actually wanting to check for a SQL null value:
If editTransactionRow.pay_id <> DbNull.Value Then
...
End If
You can also use the IsDBNull function:
If Not IsDBNull(editTransactionRow.pay_id) Then
...
If Not editTransactionRow.pay_id AndAlso String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
editTransactionRow.pay_id is Null so in fact you are doing: null.ToString() and it cannot be executed. You need to check editTransactionRow.pay_id and not editTransactionRow.pay_id.ToString();
You code should be (IF pay_id is a string):
If String.IsNullOrEmpty(editTransactionRow.pay_id) = False Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
If pay_id is an Integer than you can just check if it's null normally without String... Edit to show you if it's not a String:
If editTransactionRow.pay_id IsNot Nothing Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
If it's from a database you can use IsDBNull but if not, do not use it.
If Not IsDBNull(dr(0)) Then
use dr(0)
End If
Don't use = Nothing
or Is Nothing
, because it fails to check if the datarow value is null or not. I tried, and I make sure the above code worked.
The equivalent of null
in VB is Nothing
so your check wants to be:
If editTransactionRow.pay_id IsNot Nothing Then
stTransactionPaymentID = editTransactionRow.pay_id
End If
Or possibly, if you are actually wanting to check for a SQL null value:
If editTransactionRow.pay_id <> DbNull.Value Then
...
End If
You have to check to ensure editTransactionRow is not null and pay_id is not null.
I find the safest way is
If Not editTransactionRow.pay_id Is Nothing
It might read terribly, but the ISIL is actually very different from IsNot Nothing, and it doesn't try and evaluate the expression, which could give a null reference exception.
You have to check to ensure editTransactionRow is not null and pay_id is not null.
If Not IsDBNull(dr(0)) Then
use dr(0)
End If
Don't use = Nothing
or Is Nothing
, because it fails to check if the datarow value is null or not. I tried, and I make sure the above code worked.
If Not editTransactionRow.pay_id AndAlso String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
The equivalent of null
in VB is Nothing
so your check wants to be:
If editTransactionRow.pay_id IsNot Nothing Then
stTransactionPaymentID = editTransactionRow.pay_id
End If
Or possibly, if you are actually wanting to check for a SQL null value:
If editTransactionRow.pay_id <> DbNull.Value Then
...
End If
editTransactionRow.pay_id is Null so in fact you are doing: null.ToString() and it cannot be executed. You need to check editTransactionRow.pay_id and not editTransactionRow.pay_id.ToString();
You code should be (IF pay_id is a string):
If String.IsNullOrEmpty(editTransactionRow.pay_id) = False Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
If pay_id is an Integer than you can just check if it's null normally without String... Edit to show you if it's not a String:
If editTransactionRow.pay_id IsNot Nothing Then
stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
If it's from a database you can use IsDBNull but if not, do not use it.
Source: Stackoverflow.com