0

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);
            }
        }
    }
}
3
  • 1
    Please put some effort in making your code sample read well, and at least look as though it compiles.
    – BartoszKP
    Nov 6, 2013 at 12:43
  • "doesn't work" - define "doesn't work" please; what happens? Nov 6, 2013 at 12:55
  • btw; your query is open to SQL injection attacks. You should use a parameter rather than concatenating txtEmployeeID.Text into the SQL Nov 6, 2013 at 12:57

3 Answers 3

2

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)
4
  • Ah yes, sorry about that. When I leave it as Reader2.Read() It just skips past the while loop Nov 6, 2013 at 12:44
  • 1
    @user2960452: that's because the query is not reurning any value Nov 6, 2013 at 12:46
  • If I run the query through navicat I get a result Nov 6, 2013 at 12:48
  • As per @[Claudio-redi][1] my query was in fact invalid - I'll update in case anyone stumbles on this later the query didn't contain the % for the LIKE parameter Many thanks for your help [1]: stackoverflow.com/users/245860/claudio-redi Nov 6, 2013 at 12:54
1

This line:

while (Reader2.Read()==false)

should be:

while (Reader2.Read())
0

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.