Link to home
Start Free TrialLog in
Avatar of national_fulfillment
national_fulfillmentFlag for United States of America

asked on

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

Avatar of Ashish Patel
Ashish Patel
Flag of India image

add
myreader.read();

before you fetch the values.
ASKER CERTIFIED SOLUTION
Avatar of Ashish Patel
Ashish Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of national_fulfillment

ASKER

Thanks much.