0

I'm trying to pull data from sql database and assigning it to text boxes. But I get error when there is no data in column (null).

 string athleteId = Request.Cookies["LoggedInUser"].ToString();
             var athlete = AthleteDAL.GetAthleteByID(athleteId);
             if (athlete.AthleteFName != null)
             {
                TextBoxFirstName.Text = athlete.AthleteFName.ToString();}

I'm getting null point exception on if statement.

0

1 Answer 1

2

you need to have this if (athlete!=null && athlete.AthleteFName != null)

because athlete may be null and trying to use it (athlete.AthleteFName) is an invalid operation

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, you solved my problem. I was getting null (athlete) from database because of some error in GetAthleteByID function. Now I fixed the function too. Thanks man!

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.