vb.net 未为类型“DBNull”和字符串“”定义运算符“<>”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29405212/
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
Operator '<>' is not defined for type 'DBNull' and string ""
提问by Reshma
while using the given below code showing one error. The error is : Operator '<>' is not defined for type 'DBNull' and string "".Help me to find a proper solution. Thank You.
使用下面给出的代码显示一个错误。错误是:Operator '<>' is not defined for type 'DBNull' and string "".帮我找到合适的解决方案。谢谢你。
Code:
代码:
If sdr1.Read Then
If sdr1(1) <> "" Then
NameLabel.Text = sdr1(0).ToString + " " + sdr1(1).ToString + " " + sdr1(2).ToString
Else
NameLabel.Text = sdr1(0).ToString + " " + sdr1(2).ToString
End If
dept_id.Text = sdr1(3)
End If
sdr1.Close()
回答by Saagar Elias Hymany
You need to check whether the data you are comparing is NULL or not before you use it inside any operation:
在任何操作中使用它之前,您需要检查要比较的数据是否为 NULL:
If sdr1.Read Then
If Not IsDbNull(sdr1(1)) Then
If sdr1(1) <> "" Then
NameLabel.Text = sdr1(0).ToString + " " + sdr1(1).ToString + " " + sdr1(2).ToString
Else
NameLabel.Text = sdr1(0).ToString + " " + sdr1(2).ToString
End If
End If
dept_id.Text = sdr1(3)
End If
sdr1.Close()
or you can simply use
或者你可以简单地使用
If sdr1.Read Then
If Not IsDbNull(sdr1(1)) Then
NameLabel.Text = sdr1(0).ToString + " " + sdr1(1).ToString + " " + sdr1(2).ToString
Else
NameLabel.Text = sdr1(0).ToString + " " + sdr1(2).ToString
End If
dept_id.Text = sdr1(3)
End If
sdr1.Close()
回答by Sam Axe
Your column contains a null value. You have to check for nullness before trying to do other comparisons.
您的列包含空值。在尝试进行其他比较之前,您必须检查是否为空。
So...
所以...
If DBNull.Value Is sdr1(1) Then
' Got a null value from the database.
End If
If you don't care about whether the value is null, or empty - just want to treat them the same...
如果您不关心该值是 null 还是空 - 只想将它们视为相同...
If String.IsNullOrEmpty(sdr1(1)) Then
' The value is either null or empty.
End If
回答by Sam Axe
Use If sdr1(1) IsNot "" Thenor If Not IsDBNull(sdr1(1))Then instead for <>that will give you better result
使用If sdr1(1) IsNot "" Thenor If Not IsDBNull(sdr1(1))Then 代替<>它会给你更好的结果
回答by Sai Kalyan Kumar Akshinthala
Use String.IsNullorEmptyinstead for checking the same
使用String.IsNullorEmpty代替检查相同
Ex:-
前任:-
If Not String.IsNullOrEmpty(x) Then
'Do Something
End If
回答by user5624671
Use instead of If function
使用代替 If 函数
try
If sdr1.Read Then
If Not IsDbNull(sdr1(1)) Then
NameLabel.Text = sdr1(0).ToString + " " + sdr1(1).ToString + " " + sdr1(2).ToString
Else
NameLabel.Text = sdr1(0).ToString + " " + sdr1(2).ToString
End If
dept_id.Text = sdr1(3)
End If
Catch ex As Exception
end try

