Question

Usercontrol Code:

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

Form Code:

        using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image 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"];
                    flowLayoutPanel1.Controls.Add(usercontrol);
                }


            }
        }

With the code above i can show each LastName from database in each usercontrol's textbox. How to show the picture from database to each usercontrol's picturebox?

This is how I display a single image from database:

        using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee where ID = @a", myDatabaseConnection))
            {
                SqlCommand.Parameters.AddWithValue("@a", textBox1.Text);
                DataSet DS = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
                da.Fill(DS, "Images");
                var imagesTable = DS.Tables["Images"];
                var imagesRows = imagesTable.Rows;
                var count = imagesRows.Count;

                if (count <= 0)
                    return;
                var imageColumnValue =
                    imagesRows[count - 1]["Image"];
                if (imageColumnValue == DBNull.Value)
                    return;

                var data = (Byte[])imageColumnValue;
                using (var stream = new MemoryStream(data))
                {
                    pictureBox1.Image = Image.FromStream(stream);
                }

            }
        }  
Was it helpful?

Solution 3

Usercontrol Code:

    public void showpictures()
    {
        {
            using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
            {
                myDatabaseConnection.Open();
                using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee where LastName = @a", myDatabaseConnection))
                {

                    SqlCommand.Parameters.AddWithValue("@a", textBox1.Text);
                    DataSet DS = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
                    da.Fill(DS, "Images");
                    var imagesTable = DS.Tables["Images"];
                    var imagesRows = imagesTable.Rows;
                    var count = imagesRows.Count;

                    if (count <= 0)
                        return;
                    var imageColumnValue =
                        imagesRows[count - 1]["Image"];
                    if (imageColumnValue == DBNull.Value)
                        return;

                    var data = (Byte[])imageColumnValue;
                    using (var stream = new MemoryStream(data))
                    {
                        pictureBox1.Image = Image.FromStream(stream);
                    }


                }
            }
        }
    }

Form

            while (DR1.Read())
            {
                i++;
                UserControl2 usercontrol = new UserControl2();
                usercontrol.Tag = i;
                usercontrol.LastName = (string)DR1["LastName"];
                usercontrol.showpictures();
                flowLayoutPanel1.Controls.Add(usercontrol);
            }

OTHER TIPS

To access multiple controls at once (PictureBoxes in your case) use foreach loop.

foreach (Control control in this.Controls)
{
    if (control is PictureBox)
    {
        PictureBox pic = (PictureBox)control;
        pic.Image = Image.FromStream(stream); //something similar, this will only load the same image to every PictureBox
    }
}

Make an overload for your UserControl constructor to receive both the last name and the image bytes. Then you can do this in your loop:

            while (DR1.Read())
            {
                i++;
                UserControl2 usercontrol = new UserControl2((string)DR1["LastName"], (Byte[])DR1["Image"]);
                usercontrol.Tag = i;
                flowLayoutPanel1.Controls.Add(usercontrol);
            }

Here's the updated UserControl (with the InvokeOnClick() code from your other question). Note in the new constructor how we are creating an image from the bytes and assigning it to the PictureBox:

public partial class UserControl2 : UserControl
{

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

    public UserControl2(string LastName, byte[] data)
    {
        InitializeComponent();
        WireAllControls(this);

        this.LastName = LastName;
        try
        {
            using (var stream = new System.IO.MemoryStream(data))
            {
                pictureBox1.Image = Image.FromStream(stream);
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Error Loading Image for " + LastName);
        }
    }

    public UserControl2()
    {
        InitializeComponent();
        WireAllControls(this);
    }

    private void WireAllControls(Control cont)
    {
        foreach (Control ctl in cont.Controls)
        {
            ctl.Click += ctl_Click;
            if (ctl.HasChildren)
            {
                WireAllControls(ctl);
            }
        }
    }

    private void ctl_Click(object sender, EventArgs e)
    {
        this.InvokeOnClick(this, EventArgs.Empty); 
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top