Avatar of dpdmembers
dpdmembers
Flag for Barbados asked on

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.
ASP.NETLINQ Query.NET Programming

Avatar of undefined
Last Comment
dpdmembers

8/22/2022 - Mon
Éric Moreau

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
dpdmembers

ASKER
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 Moreau

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
dpdmembers

ASKER
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.
dpdmembers

ASKER
attached file For your review
capture.png
ASKER CERTIFIED SOLUTION
Éric Moreau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dpdmembers

ASKER
Looks promising..will test it over the weekend and get back to you.  Thanks.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.