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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Ashish PatelConnect With a Mentor Commented:
Also while using this

myreader.Read();

always use myreader.HasRows() to check if the data has records in it.
0
 
Ashish PatelCommented:
add
myreader.read();

before you fetch the values.
0
 
national_fulfillmentAuthor Commented:
Thanks much.
0
All Courses

From novice to tech pro — start learning today.