如何在数据集中找到NULL? [英] How to find a NULL in a dataset ?

查看:114
本文介绍了如何在数据集中找到NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim sqlStr As 字符串 = " SELECT * FROM urlTable WHERE urlName ='" & CStr (comboSearch.SelectedItem)& "'"

Dim dataAdapter As OleDb.OleDbDataAdapter(sqlStr,connStr)


dataAdapter.Fill(dt)


dataAdapter.Dispose()

我已声明所有必要的variblas并且代码工作正常

现在让我说我给这个命令...

urlStart =
CStr (dt.Rows(0)( " urlStart" ))

现在,当urlStart中有值时,代码将正常运行...但是当它为NULL时会生成错误...

我可以找到urlStart是否具有NULL值的命令是什么,如果不是只运行上面的命令?

我正在寻找类似的有效代码

如果dt.Rows(0)(" urlStart")<> NULL然后
urlStart = CStr (dt.Rows(0)( " urlStart" ))
Else
urlStart =""
End if

我想要的是如何在没有第一个的情况下找到它是否为NULL?

我也得到这个警告...
警告1:使用变量'urlStart'在为其分配值之前。在运行时可能会产生空引用异常。

Thanx很多:D

解决方案

研究DBNull。这是VB帮助中的示例。但总的来说,你的暗示可能会走向错误的方向。



如果你可以创建一个类型化的数据集,那就这样做。 / p>

向数据适配器添加一个选择qry。然后您可以这样访问:

MyDataAdapter.GetByCustomerId(" C123456")

其他,这个变得丑陋了
" CStr (dt.Rows(0)( " urlStart" ))"

另一方面你可以像这样引用:

MyCurrentRow.urlstart =" somevalue"










< td> 私人 < font style ="color:blue"> Sub OUtputLabels(dt As DataTable)







< td> 对于 每个 row As DataRow dt.Rows



























































Dim label As < font style ="font-size:11px"> 字符串
'迭代表格行
Dim labelLen As 整数 < font style ="font-size:11px">
label = String 。Empty
标签+ = AddFieldValue(标签,行, " Title"
label + = AddFieldValue(label,row, " FirstName"
label + = AddFieldValue( label,row, " MiddleInitial"
label + = AddFieldValue(label,row, " LastName"
label + = AddFieldValue( label,row, " Suffix"
label + = vbCrLf
label + = AddFieldValue(label,row, " Address1"
label + = AddFieldValue(label,row, " AptNo"
label + = vbCrLf
labelLen = Len(label)
label + = AddFieldValue(label,row, " Address2"
If Len(label)<> labelLen 然后 label + = vbCrLf
label + = AddFieldValue(label,row, " City"
label + = AddFieldValue(label,row , " State"
label + = AddFieldValue(label,row, " Zip"
Console.WriteLine(label)
Console.WriteLine()
下一步
结束 Sub
私人 功能 AddFieldValue(标签 As String ,row As DataRow,_
fieldName As String As 字符串
如果 DbNull.Value.Equals(行.Item(fieldName)) 然后
返回 CStr (row.Item(fieldName))& " "
其他
返回 Nothing
结束 如果
结束 功能

Dim sqlStr As String = "SELECT * FROM urlTable WHERE urlName='" & CStr(comboSearch.SelectedItem) & "'"

Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)

dataAdapter.Fill(dt)

dataAdapter.Dispose()

I have declared all the neccessary variblas and the code is working fine

Now lets say i give this command...

urlStart =
CStr(dt.Rows(0)("urlStart"))

Now the code will work fine when there is a value in urlStart...but will generate an error when it is NULL...

what is the command where i can find whether urlStart has a NULL value and if not only run the above command?

Im looking for a valid code something like 

If dt.Rows(0)("urlStart") <> NULL Then
    urlStart = CStr(dt.Rows(0)("urlStart"))
Else
    urlStart = ""
End if

What i want is how to find whether it is NULL or not without first??

I get this warning too...
Warning 1 : Variable 'urlStart' is used before it has been assigned a value. A null reference exception could result at runtime.

Thanx a lot :D

解决方案

 Research DBNull. This is a sample from VB Help. But in general, you may be going into the wrong direction with your implimentation.

 

If you can create a typed dataset, do so.

Add a select qry to the data adapter.Then you can access like so:

MyDataAdapter.GetByCustomerId("C123456")

Otherwise, This gets ugly
"CStr(dt.Rows(0)("urlStart"))"

on the other hand you can reference like so:

MyCurrentRow.urlstart = "somevalue"




Private Sub OUtputLabels(dt As DataTable)  
   Dim label As String   
 
   ' Iterate rows of table  
   For Each row As DataRow In dt.Rows  
      Dim labelLen As Integer 
      label = String.Empty  
      label += AddFieldValue(label, row, "Title")  
      label += AddFieldValue(label, row, "FirstName")  
      label += AddFieldValue(label, row, "MiddleInitial")  
      label += AddFieldValue(label, row, "LastName")  
      label += AddFieldValue(label, row, "Suffix")  
      label += vbCrLf  
      label += AddFieldValue(label, row, "Address1")  
      label += AddFieldValue(label, row, "AptNo")  
      label += vbCrLf  
      labelLen = Len(label)  
      label += AddFieldValue(label, row, "Address2")  
      If Len(label) <> labelLen Then label += vbCrLf  
      label += AddFieldValue(label, row, "City")  
      label += AddFieldValue(label, row, "State")  
      label += AddFieldValue(label, row, "Zip")  
      Console.WriteLine(label)  
      Console.WriteLine()  
   Next 
End Sub 
 
Private Function AddFieldValue(label As String, row As DataRow, _  
                          fieldName As StringAs String 
   If Not DbNull.Value.Equals(row.Item(fieldName)) Then 
      Return CStr(row.Item(fieldName)) & " " 
   Else 
      Return Nothing 
   End If 
End Function 


这篇关于如何在数据集中找到NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆