SQLDataReader error in C# ASP.Net

I'm sure this is going to be completely obvious to someone (except for me). I use the sqldatareader to populate a formview. I know the record is being returned because the form will load if comment out the line of code it's hanging on. I'm trying to set a string equal to ones of the fields being returned in the reader and I get the error nothing to return.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string uid = (string)(Session["uid"]);
            string emailstat;
            SqlConnection myconnection;
            SqlCommand mycommand;
            SqlDataReader myreader;
            //SqlDataReader myreader;
 
            myconnection = new SqlConnection();
            myconnection.ConnectionString = ConfigurationManager.ConnectionStrings["BBC_ViewerRelationsConnectionString"].ConnectionString;
            mycommand = new SqlCommand();
            mycommand.CommandText = "select * from [tblViewRelations] where [uniqueid]=" + uid;
            mycommand.CommandType = CommandType.Text;
 
            try
            {
                mycommand.Connection = myconnection;
                mycommand.Connection.Open();
                myreader = mycommand.ExecuteReader(CommandBehavior.CloseConnection);
// this next line is the line I'm having problems with
                emailstat = myreader["PendingEmail"].ToString();
                FormView1.DataSource = myreader;
                FormView1.DataBind();                
                mycommand.Dispose();
                myconnection.Dispose();
                
                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();
                    Response.Write(pendingemailbody);
                    TextBox emailbodylab = (TextBox)FormView1.Row.FindControl("TextBox1");
                    emailbodylab.Text = pendingemailbody;
                }
 
            }
 
            catch (Exception ex)
            {
                //Label emailstatus = (Label)FormView1.Row.FindControl("statuslabel");
                Response.Write(ex.ToString());
            }
 
        }
    }

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.

Ashish PatelCommented:
add
myreader.read();

before you fetch the values.
0
Ashish PatelCommented:
Also while using this

myreader.Read();

always use myreader.HasRows() to check if the data has records in it.
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 much.
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
ASP.NET

From novice to tech pro — start learning today.