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;

                OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Documents\Databases\employeedb.accdb");
                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;
                        MessageBox.Show("Could not find the employee you are looking for.");
            catch(Exception ee)

How do I fix this?

2 Years
Discussion Span
Last Post by hericles

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.