Invalid attempt to access a field before calling Read ()

Nov 9 2009 2:11 PM
Guys, am try to read a fields from two tables (memberinfo and exemptions) in my database and I am getting this annoying error: Invalid attempt to access a field before calling Read () even though i had used the same approach before with no error. Below is the code which worked

public void loaddetails()
        {

            MySqlDataReader reader = null;

            try
            {
                accessDB.connection();
                //this.reset();

                String search = "SELECT number, memberSname,memberFname,memberOname,memberDOB FROM memberInfo WHERE memberid = '" + txtmid.Text + "'";
                accessDB.myCmd(search).ExecuteNonQuery();

                reader = accessDB.myCmd(search).ExecuteReader();

                reader.Read();
                txtmeid.Text = reader["number"].ToString();
                txtmeSname.Text = reader["memberSname"].ToString();
                txtmeFname.Text = reader["memberFname"].ToString();
                txtmeOname.Text = reader["memberOname"].ToString();
                dob.Text = reader["memberDOB"].ToString();
                reader.Close();
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
And below is the one throwing the error:Invalid attempt to access a field before calling Read ().

public void loaddetails1()
        {

            MySqlDataReader reader1 = null;
            try
            {
                accessDB.connection();
                //this.reset();

                String load = "SELECT memberinfo.memberDOB, exemptions.number,surname, firstname, othername, exempt, exempt1, exempt2 FROM memberinfo, exemptions WHERE  exemptions.Id = '" + txtmeid.Text + "'";
                accessDB.myCmd(load).ExecuteNonQuery();

                reader1 = accessDB.myCmd(load).ExecuteReader();
               
                reader1.Read();
                dob.Text = reader1["memberDOB"].ToString();
                txtmeid.Text = reader1["number"].ToString();
                txtmeSname.Text = reader1["Surname"].ToString();
                txtmeFname.Text = reader1["Firstname"].ToString();
                txtmeOname.Text = reader1["Othername"].ToString();
                txtexempt.Text = reader1["exempt"].ToString();
                txtexempt1.Text = reader1["exempt1"].ToString();
                txtexempt2.Text = reader1["exempt2"].ToString();
                reader1.Close();


            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Please I want to know why the first one worked but not the second one and how to make the second one work.

Answers (4)