tags:

views:

13993

answers:

6

OK,

It sounds like piece of cake, but please accept my ignorance and help me with this.

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 vb thhrows an exception? Is there something wrong with this code?

Thanks

+4  A: 

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.

Daok
Well, I can't check this:If String.IsNullOrEmpty(editTransactionRow.pay_id1) = False Then stTransactionPaymentID = editTransactionRow.pay_id1 'Check for null value End If
read what I have wrote. You can compare your INT if it's false WITHOUT using string...
Daok
+1  A: 

You have to check to ensure editTransactionRow is not null and pay_id is not null.

Zachary Yates
A: 
If Not editTransactionRow.pay_id AndAlso String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then
    stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
Vincent
+1  A: 

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
Garry Shutler
+3  A: 

If you are using a strongly-typed dataset then you should do this:

If Not ediTransactionRow.Ispay_id1Null Then
    'Do processing here'
End If

The reason you are getting the error is that a strongly-typed data set retrieves the underlying value and exposes the conversion through the property. For instance here is essentially what is happening

Public Property pay_Id1 Then
   Get
     return DirectCast(me.GetValue("pay_Id1", short)
   End Get
   'Abbreaviated for clarity'
End Property

What happens is that the GetValue method is returning DBNull which cannot be converted to a short.

Micah
A: 
 If Short.TryParse(editTransactionRow.pay_id, New Short) Then editTransactionRow.pay_id.ToString()
Shawn Simon