Problem setting value of textbox inside Formview C# asp.net

I use the same code in other pages and it works fine. I'm trying to set the value of a textbox in a formview to be equal to the data returned from a query. I get the following error message:
System.NullReferenceException: Object reference not set to an instance of an object
anybody have a clue why?
if (emailstat == "Y")
                {
                    mycommand.CommandText = "Display_Pending";
                    mycommand.CommandType = CommandType.StoredProcedure;
                    mycommand.Connection = myconnection;
                    mycommand.Connection.Open();
                    mycommand.Parameters.Add(
                    new SqlParameter("@ID", uid));
                    myreader = mycommand.ExecuteReader();
                    myreader.Read();
                    string pendingemailbody;
     pendingemailbody = myreader["EmailBody"].ToString();
     TextBox emailbodylab = (TextBox)FormView1.FindControl("TextBox1");
          //the following line is where the exception is thrown.        emailbodylab.Text = pendingemailbody;
                }

Open in new window

national_fulfillmentAsked:
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.

burakiewiczCommented:
if the control is not found and emailbodylab is null you will get that error
0
national_fulfillmentAuthor Commented:
usually the line I bug out on would be
TextBox emailbodylab = (TextBox)FormView1.FindControl("TextBox1");
when the control can' be found.
     
0
burakiewiczCommented:
so your problem is with this
TextBox emailbodylab = (TextBox)FormView1.FindControl("TextBox1");

not this
emailbodylab.Text = pendingemailbody;
0
burakiewiczCommented:
try this in your code and see if you get an error

TextBox emailbodylab = (TextBox)FormView1.FindControl("TextBox1");
if (emailbodylab != null)
{
    emailbodylab.Text = pendingemailbody;
}
0

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
national_fulfillmentAuthor Commented:
Thanks for responding I'm an idiot. The code was right (and so is what you posted too) but where I had it at in the program was wrong. I was trying to access the values of textbox1 before I loaded the formview with data. Dah... Thanks again.
0
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
.NET Programming

From novice to tech pro — start learning today.