检查绑定的datatable为空值vb.net [英] check bound datatable for null value vb.net
问题描述
我目前正在使用带有sql后端的vb.net窗体应用程序。我有两个数据表绑定到两个独立的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.
推荐答案
使用 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
这篇关于检查绑定的datatable为空值vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!