简体   繁体   English

如何在VB.NET中检查Null值

[英]How to check for a Null value in VB.NET

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. 现在,当editTransactionRow.pay_id为Null时,Visual Basic会抛出异常。 Is there something wrong with this code? 这段代码有问题吗?

The equivalent of null in VB is Nothing so your check wants to be: VB中的null相当于Nothing所以你的检查想成为:

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: 或者,如果您确实想要检查SQL null值:

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. editTransactionRow.pay_id是Null所以实际上你正在做:null.ToString()并且它无法执行。 You need to check editTransactionRow.pay_id and not editTransactionRow.pay_id.ToString(); 您需要检查editTransactionRow.pay_id而不是editTransactionRow.pay_id.ToString();

You code should be (IF pay_id is a string): 你的代码应该是(IF pay_id是一个字符串):

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: 如果pay_id是一个Integer而不是你可以检查它是否正常,没有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. 如果它来自数据库,您可以使用IsDBNull,但如果不是,请不要使用它。

If you are using a strongly-typed dataset then you should do this: 如果您使用的是强类型数据集,那么您应该这样做:

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

You are getting the error because 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
   'Abbreviated for clarity
End Property

The GetValue method is returning DBNull which cannot be converted to a short. GetValue方法返回DBNull,无法将其转换为short。

You can also use the IsDBNull function: 您还可以使用IsDBNull函数:

If Not IsDBNull(editTransactionRow.pay_id) Then
...
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. 不要使用= NothingIs Nothing ,因为它无法检查数据行值是否为null。 I tried, and I make sure the above code worked. 我试过了,我确保上面的代码有效。

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. 它可能读得非常糟糕,但ISIL实际上与IsNot Nothing非常不同,并且它不会尝试评估表达式,这可能会产生空引用异常。

您必须检查以确保editTransactionRow不为null且pay_id不为null。

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
If Not editTransactionRow.pay_id AndAlso String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then
    stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If
 If Short.TryParse(editTransactionRow.pay_id, New Short) Then editTransactionRow.pay_id.ToString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM