I have a small program that queries a database and retrieve some information about employees. The only problem I have is that when I search for the employee my program never finds any employee after the first employee.

Here is my code:

private void buttonSearch_Click(object sender, EventArgs e)
        {
            string sname = textBoxNameIN.Text;
            string slname = textBoxSNameIN.Text;

            string name;
            string lname;
            string gender;
            string id;
            string province;
            string city;
            string addr;
            string field;
            string degree;

            try
            {
                OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Documents\Databases\employeedb.accdb");
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("select * from Employee", conn);
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    name = reader["fname"].ToString();
                    lname = reader["lname"].ToString();
                    gender = reader["gender"].ToString();
                    id = reader["iden"].ToString();
                    province = reader["province"].ToString();
                    city = reader["city"].ToString();
                    addr = reader["addr"].ToString();
                    field = reader["field"].ToString();
                    degree = reader["degree"].ToString();

                    if ((name == sname) && (lname == slname))
                    {
                        textBoxNameOut.Text = name;
                        textBoxLNameOut.Text = lname;
                        textBoxGenderOut.Text = gender;
                        textBoxIDOut.Text = id;
                        textBoxProvinceOut.Text = province;
                        textBoxCityOut.Text = city;
                        textBoxAddrOut.Text = addr;
                        textBoxFieldOut.Text = field;
                        textBoxDegreeOut.Text = degree;
                        break;
                    }
                    else
                    {
                        MessageBox.Show("Could not find the employee you are looking for.");
                        break;
                    }
                }
                conn.Close();
            }
            catch(Exception ee)
            {
                MessageBox.Show(ee.Message.ToString());
            }
        }

How do I fix this?

Why are you doing it this way? If you want a particular employee and you're entering the name, why aren't you adding that information to the SQL query and retrieving just one row?

As it is, the code is failing on any employee beyond the first one because of the else statement you have. If the returned name doesn't match the entered name, which it won't for any query which doesn't use the first record's name, you show an error and break out.
You shouldn't be doing that, you're forcing your code to only run once through the reader.
I would suggest using the name in the SQL query as parameter. You're returning records that you don't need. And then you're allocating the returned data to variables before you know if you actually need it. If you want to keep the code you, fix the else problem and check for the name matching FIRST and then allocate data to variables if you have a match.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.