如何在 VB.NET 中检查 Null 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/378225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:58:01  来源:igfitidea点击:

How to check for a Null value in VB.NET

vb.netdataset

提问by Garry Shutler

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_idis Null Visual Basic throws an exception. Is there something wrong with this code?

现在,什么时候editTransactionRow.pay_id为 Null Visual Basic 会引发异常。这段代码有什么问题吗?

采纳答案by Micah

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 方法正在返回无法转换为 short 的 DBNull。

回答by Garry Shutler

The equivalent of nullin VB is Nothingso your check wants to be:

null在 VB 中的等价物是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 空值:

If editTransactionRow.pay_id <> DbNull.Value Then
    ...
End If

回答by Patrick Desjardins

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();

editTransactionRow.pay_id 是 Null 所以实际上你在做: null.ToString() 并且它不能被执行。您需要检查 editTransactionRow.pay_id 而不是 editTransactionRow.pay_id.ToString();

You code should be (IF pay_id is a string):

您的代码应该是(如果 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 是一个整数,那么您可以在没有字符串的情况下检查它是否正常为空...编辑以显示它是否不是字符串:

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,但如果不是,请不要使用它。

回答by Aaron G

You can also use the IsDBNull function:

您还可以使用 IsDBNull 函数:

If Not IsDBNull(editTransactionRow.pay_id) Then
...

回答by Arunsai

If Not IsDBNull(dr(0)) Then
    use dr(0)
End If

Don't use = Nothingor 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.

不要使用= Nothingor Is Nothing,因为它无法检查数据行值是否为空。我试过了,我确保上面的代码有效。

回答by stuartdotnet

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 非常不同,它不会尝试评估表达式,这可能会产生空引用异常。

回答by Zachary Yates

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

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

回答by D Infosystems

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

回答by Vincent

If Not editTransactionRow.pay_id AndAlso String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then
    stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value
End If

回答by Shawn

 If Short.TryParse(editTransactionRow.pay_id, New Short) Then editTransactionRow.pay_id.ToString()