1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
2) There is no point in looping through entries if all you are going to do is overwrite the last one in each textbox.
3) Please, dispose of your objects!
4) Why are you doing the last bit? All it will do is waste time...
5) Never, ever use "SELECT * ..." and then reference the returned data by numeric indexes. You do not know what order Sql Server will return columns in - if the table changes, your program fails. and not for any obvious reason. Nasty to find, and fiddly to fix. It is bad practice to always return every field anyway - it wastes bandwidth which can cause significant slowdown with large amounts of data.
6) Stop using the Visual Studio default names for controls. You may remember that textBox7 is the postcode today, but in a weeks time, you will have to hunt about to find out which one it is like the rest of us. Use meaningful names!
7) It is also a good idea to use UPPER CASE for Sql Command element, and lower case for fields - it makes it easier to follow the SQL command when you are reading it.
8) Try to think about your users: "not found" is not a helpfull error message - it doesn't tell them what is wrong, or what they should do to avoid it in future.
Try:
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Data Source=CBP\\SQLEXPRESS;Initial Catalog=ECG;Integrated Security=True"))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT middle, last FROM Table2 WHERE first=@FIRST", con))
{
cmd.Parameters.AddWithValue("@FIRST", tbFirstName.Text.Trim());
using (SqlDataReader rdr = cmd.ExecuteReader())
{
if (rdr.Read())
{
tbMiddleName.Text = rdr.GetString("middle");
tbLastName.Text = rdr.GetString("last");
}
else
{
MessageBox.Show(string.Format("No data was found for anyone with the first name of \"{0}\"",tbFirstName.Text.Trim()) );
}
}
}
}
}
It may not cure your immediate problem, but it goes a fair way toward getting rid of it.
When you have done this, which line is it erroring on?