1

UserControl:

private string lastName;
public string LastName
{
get { return lastName; }
set
{
    lastName = value;
    lastNameTextBox.Text = value;
}
}

Form:

using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LasatName from Employee", myDatabaseConnection))
            {
                int i = 0;
                SqlDataReader DR1 = SqlCommand.ExecuteReader();
                while (DR1.Read())
                {
                    i++;
                    UserControl2 usercontrol = new UserControl2();
                    usercontrol.Tag = i;
                    usercontrol.LastName = (string)DR1["LastName"];
                    usercontrol.Click += new EventHandler(usercontrol_Click); 
                    flowLayoutPanel1.Controls.Add(usercontrol);
                }
            }
        }

The form loads the records from database and display each LastName in each usercontrols' textbox that created dynamically. How to show additonal information such as address in form's textbox when a dynamic-usercontrol is click?

Attempt:

    private void usercontrol_Click(object sender, EventArgs e)
    {
        using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand mySqlCommand = new SqlCommand("Select Address from Employee where LastName = @LastName ", myDatabaseConnection))

            {
                UserControl2 usercontrol = new UserControl2();
                mySqlCommand.Parameters.AddWithValue("@LastName", usercontrol.LastName;
                SqlDataReader sqlreader = mySqlCommand.ExecuteReader();

                if (sqlreader.Read())
                {
                    textBox1.Text = (string)sqlreader["Address"];
                }

            }
        }
    }
Karlx Swanovski
  • 2,869
  • 9
  • 34
  • 67
  • And the attempt didn't work why? There isn't near enough information here for us to even have an idea of what you're trying to do -**and more code isn't going to be the answer.** Explain what you want, what you're doing, and what's not working. – Mike Perrenoud Jun 06 '13 at 16:49
  • @Michael - http://stackoverflow.com/questions/16894918/display-a-number-of-usercontrol-based-on-the-number-of-records-in-the-database?noredirect=1#comment24378920_16894918 – Karlx Swanovski Jun 06 '13 at 17:09

2 Answers2

1

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;
}
Igor
  • 15,833
  • 1
  • 27
  • 32
0

Inside your user control UserControl2 (in the constructor or InitializeComponent method) you should forward the click event to the potential listeners.

Something like that:

public UserControl2()
{
    ...
    this.lastNameTextBox.Click += (s, a) => OnClick(a);
    ...
}
Pragmateek
  • 13,174
  • 9
  • 74
  • 108