检查空值的绑定数据表 vb.net [英] check bound datatable for null value vb.net
问题描述
我目前正在使用带有 sql 后端的 vb.net windows 窗体应用程序.我有两个数据表,它们绑定到两个独立的 sql 语句,并且都返回两个非常简单的数据表.我只需要独立检查两个表中的任何一个中是否存在空值.第一个查询返回单个值,实际上是单个单元格.我一直在 dgv 中使用以下代码来检查 null
I am currently working in vb.net windows form applications with an sql back end. I have two datatables that are bound to two independent sql statements and both return two very simple datatables. I just need to check to see if there is a null value in either of the two tables, independently. The first query returns a single value, literally a single cell. I have always used the following code in a dgv to check for null
Dim Check As Integer = dt.Rows(1).cells(1).value
然而这在这里不起作用,这似乎是因为数据类型是整数.通常使用 DGV,我正在执行点击事件,并从最终用户可以看到的 dgv 中拉取.
However this is not working here, it seems to be because the data type is an integer. Usually with a DGV I am doing a click event and am pulling from a dgv an end user can see.
第二个 sql 查询只有 2 个单元格.因为它有 2 行和 1 列,我需要检查两个单元格以确保它们都不为空.
The second sql query is just 2 cells. As in it has 2 rows and one column and I need to check both cells to make sure neither of them are null.
我一直在网上四处寻找,但找不到任何对我有用的东西.
I have been looking around online but I cant find anything that seems to work for me.
------------------------更新----------------------------
------------------------update----------------------------
Public Shared Function ToInt32(Value As Object) As Integer
If DBNull.Value.Equals(Value) Then
Return 0
Else
Return Convert.ToInt32(Value)
End If
End Function
Dim ncheck As Integer
ncheck = SafeConvert.ToInt32(dt.Rows(1)(1))
我将如何在 if 语句中使用此 ncheck 来运行退出子程序和消息框?在我看来,如果 ncheck 不是整数,它只会让我崩溃.
How would i use this ncheck in a if statement to run an exit sub and a message box? It seems to me if ncheck is not an integer it is just going to crash on me.
推荐答案
use DbNull.Value
:
If Not DbNull.Value.Equals(dt.Rows(1).Item(1).value) Then
' do something
Else
' do something else
End If
或使用
If dt.Rows(1).Item(1).value=DbNull.Value
这篇关于检查空值的绑定数据表 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!