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.
Loading
Kirtan PatelPosted Nov 10, 2009, 11:56 AM
Send Me your Project Files (zip) in Mail or Just Upload Project Files(Zip)
on http://mediafire.com
and mail me the Download link at [email protected]
will resolve your problem and send your project back after correcting the code :)
Nilanka DharmadasaPosted Nov 9, 2009, 11:03 PM
Why do you need this line?
accessDB.myCmd(load).ExecuteNonQuery();
Remove this and check.
If you still don;t get data that means your query doesnt return any data.
JONAS BOATENGPosted Nov 9, 2009, 2:51 PM
Kirtan PatelPosted Nov 9, 2009, 2:24 PM
you should Write code Like Below One Write Read() method in While loop
if my answer helps you then check Do you like this answer check box :)