user2960452
user2960452

Reputation: 41

Invalid attempt to access a field before calling Read() C#

I am trying to create an application that allows someone to search for an exact result via the ID or if you don't know the ID you can search for part of their name to return accounts that match.

E.g Bob Jones is ID A01, you can either search for A01 to return Bob Jones or search B to return Bob Jones, Bradley Smith e.t.c...

I have coded the ID part just fine for some reason the second part doesn't work when it's practically the same code.

private void btnSaveEmployees_Click(object sender, EventArgs e)
{
    {
        //First search for the entered text to see if it is an ID
        DataBase db = new DataBase(); //Construct a new database.
        db.OpenConnection(); //Open our Database connection

        MySqlCommand mysqlCmd = new MySqlCommand("SELECT * FROM `employees` WHERE `employee_ID` = @employeeID LIMIT 1", db.connection);
        mysqlCmd.Parameters.AddWithValue("@employeeID", txtEmployeeID.Text);

        MySqlDataReader reader = mysqlCmd.ExecuteReader(); //Construct a reader 

        if (reader.Read())
        {
            //Employee ID has been found so lets update the update form.
            Form employee = new UpdateEmployee(reader["firstName"].ToString(), reader["lastName"].ToString(), reader["contactNumber"].ToString(), reader["employee_access_level"].ToString());
            employee.Show();
        }
        else
        {
            db.CloseConnection();
            db.OpenConnection();
            MySqlDataReader Reader2;
            MySqlCommand mysqlCmd2 = new MySqlCommand("SELECT firstName, LastName FROM `employees` WHERE `firstName` LIKE '"+txtEmployeeID.Text+"';", db.connection);
            //mysqlCmd2.Parameters.AddWithValue("@textbox", txtEmployeeID.Text);
            Reader2 = mysqlCmd2.ExecuteReader(); //Construct a reader

            while (Reader2.Read()==false)
            {
                listboxFindEmployees.Visible = true;
                string thisrow = "";

                for (int i = 0; i < Reader2.FieldCount; i++)
                    thisrow += Reader2.GetValue(i).ToString() + ",";

                listboxFindEmployees.Items.Add(thisrow);
            }
        }
    }
}

Upvotes: 0

Views: 1964

Answers (3)

Claudio Redi
Claudio Redi

Reputation: 68410

At first sight you need to change

while (Reader2.Read()==false)

by

while (Reader2.Read())

You while condition is only allowing to enter when there is no more records to read so it will fail to execute the logic inside.

Your logic has some extra problems you should address

  • You're exposed to sql injection attacks building queries using string concatenations. Use parameters instead.
  • You have to release every instance that implement IDisposable interface (MySqlConnection, MySqlDataReader, MySqlCommand)

Upvotes: 2

Abbas
Abbas

Reputation: 14432

This line:

while (Reader2.Read()==false)

should be:

while (Reader2.Read())

Upvotes: 1

usr
usr

Reputation: 171218

You haven't disposed of the old DataReader. I suggest that you rigorously change everything to use using blocks. That gets rid of all of these problems. (Also enclose the connection in using-blocks. If an exception was thrown at the wrong moment, you'd leak a connection or a reader).

Upvotes: 0

Related Questions