0

I have the following code:

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

When I execute it, it throws an object reference not set to an instance of an object error on getProspect.user_id. Why is it doing this? Shouldn't the fact that I'm verifying it exists using IsDBNull first keep this from happening?

1 Answer 1

1

DBNull is not the same as Nothing, and what you have is Nothing. FirstOrDefault, as the name suggests, returns the first item or the default value, which is Nothing for reference types - never 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.