TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
JONAS BOATENG
NA
24
0
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.
Reply
Answers (
4
)
from a website into the database
Help Me! C# Question