First. In your first code fragment, you execute "Select ID from ...", but expect to find field "LastName" in the reader - not going to work.
Second. If you know that you will need more information from Employee
table, I suggest that you store it in the same UserControl
where you put the LastName
. Execute "Select * from ...", add another field and property to UserControl2
and assign it in the Read
loop:
usercontrol.Address = (string)DR1["Address"];
Third. In your second code fragment, use
UserControl2 usercontrol = (UserControl2)sender;
instead of
UserControl2 usercontrol = new UserControl2();
because newly created user control will not have any LastName
assigned.
Then:
private void usercontrol_Click(object sender, EventArgs e)
{
UserControl2 usercontrol = (UserControl2)sender;
textBox1.Text = usercontrol.Address;
}