我有以下代码:
Dim getProspect = (From p In dbContext.IRF_Prospects _
Where p.url = prospect_url _
Select p).FirstOrDefault
' If they have a record...
If Not IsDBNull(getProspect) Then
If IsDBNull(getProspect.user_id) Then
' Prepopulate the form with their information.
txtFirst.Text = getProspect.first_name
Else
' Redirect them to login.
Response.Redirect("login.aspx")
End If
当我执行它时,它会在getProspect.user_id
上抛出一个未设置为对象错误实例的对象引用。它为什么这样做?我不应该使用IsDBNull
验证它是否存在这一事实不会发生这种情况吗?
答案 0 :(得分:1)
DBNull
与Nothing
不同,您所拥有的是Nothing
。顾名思义,FirstOrDefault
会返回第一项或默认值,对于引用类型,它是Nothing
- 从不DBNull
。
Dim getProspect = (From p In dbContext.IRF_Prospects _
Where p.url = prospect_url _
Select p).FirstOrDefault
' If they have a record...
If getProspect IsNot Nothing Then
If IsDBNull(getProspect.user_id) Then
' Prepopulate the form with their information.
txtFirst.Text = getProspect.first_name
Else
' Redirect them to login.
Response.Redirect("login.aspx")
End If