NullReferenceException Problem

I am retrieving some data form a table using linq.  I am pulling that info into text boxes I have on a form.  But because some of the fields may be null and there is no way to know which ones will be null at any given time, thus I keep getting a NullReferenceException or an Object reference not set to an instance of an object error.  I have tried everything to try to test the field to see if it is null but nothing has work.
dpdmembersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
can you show us some code for which you have issues?

are you using VS2015 and targeting one of the last Framework? If yes, you might want to try the Elvis operator: https://msdn.microsoft.com/en-us/library/dn986595.aspx
dpdmembersAuthor Commented:
I am using VS2012.

 Dim xNo = (From xReg In _Context.tbl_Peoples
                          Where xReg.str_Nat = Me.NatTextBox.Text
                          Select xReg)

If xNo.Count > 0 Then
            For Each NatReg In xNo

                NameTextBox.Text = NatReg.str_ForeName2

            Next

End If


If NatReg.str_ForeName2 is null in the database table I get the errors.
Éric MoreauSenior .Net ConsultantCommented:
can you try:
if string.isnullorwhitespace(NatReg.str_ForeName2) then
   NameTextBox.Text = string.empty
else
   NameTextBox.Text = NatReg.str_ForeName2
end if

Open in new window

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

dpdmembersAuthor Commented:
I tried that already.  Does not even get past the IF.

I got the error Object reference not set to an instance of an object.
dpdmembersAuthor Commented:
attached file For your review
capture.png
Éric MoreauSenior .Net ConsultantCommented:
that means that your NatReg is empty:

if NatReg is nothing orelse  string.isnullorwhitespace(NatReg.str_ForeName2) then
   NameTextBox.Text = string.empty
else
   NameTextBox.Text = NatReg.str_ForeName2
end if

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dpdmembersAuthor Commented:
Looks promising..will test it over the weekend and get back to you.  Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.