Adding value in the picturebox

0

I'd like to know if you have any way to make it work here

int chck = 0;
int fid = 0;
        while (chck < 150)
        {
            try
            {
                sqlcon.Open();
                string sql2 = "SELECT img FROM clienteimg WHERE ((clienteid = '" + cid + "') AND (id = '" + fid + "'))";
                SqlCommand cmd2 = new SqlCommand(sql2, sqlcon);
                SqlDataReader dr2 = cmd2.ExecuteReader();
                dr2.Read();
                byte[] imgLoc = (byte[])dr2[0];
                sqlcon.Close();
                MemoryStream mstream = new MemoryStream(imgLoc);
                pb1.Image = Image.FromStream(mstream);
            }
            catch (Exception)
            {
                sqlcon.Close();
                sqlcon.Open();
                string foid = "9";
                string sql3 = "SELECT img FROM ClienteImgDB WHERE id = '" + foid + "'";
                SqlCommand cmd3 = new SqlCommand(sql3, sqlcon);
                SqlDataReader dr3 = cmd3.ExecuteReader();
                dr3.Read();
                byte[] imgLoc = (byte[])dr3[0];
                sqlcon.Close();
                MemoryStream mstream = new MemoryStream(imgLoc);
                pb1.Image = Image.FromStream(mstream);
            }
            chck++;
            fid++;
        }

int chck = 0;
int fid = 0;
        while (chck < 150)
        {
            try
            {
                sqlcon.Open();
                string sql2 = "SELECT img FROM clienteimg WHERE ((clienteid = '" + cid + "') AND (id = '" + fid + "'))";
                SqlCommand cmd2 = new SqlCommand(sql2, sqlcon);
                SqlDataReader dr2 = cmd2.ExecuteReader();
                dr2.Read();
                byte[] imgLoc = (byte[])dr2[0];
                sqlcon.Close();
                MemoryStream mstream = new MemoryStream(imgLoc);
                pb'"+fid+"'.Image = Image.FromStream(mstream);
            }
            catch (Exception)
            {
                sqlcon.Close();
                sqlcon.Open();
                string foid = "9";
                string sql3 = "SELECT img FROM ClienteImgDB WHERE id = '" + foid + "'";
                SqlCommand cmd3 = new SqlCommand(sql3, sqlcon);
                SqlDataReader dr3 = cmd3.ExecuteReader();
                dr3.Read();
                byte[] imgLoc = (byte[])dr3[0];
                sqlcon.Close();
                MemoryStream mstream = new MemoryStream(imgLoc);
                pb'"+fid+"'.Image = Image.FromStream(mstream);
            }
            chck++;
            fid++;
        }

For every time I loop the "pb" add 1 more to fill the 150 pictureboxes I have

    
asked by anonymous 05.02.2018 / 03:18

1 answer

1

You can use the Find method of the control or form in which the picturebox are inserted:

for (int i =0; i < 5; i++)
{
    ((PictureBox)this.Controls.Find("pb" + i, true)[0]).Image = null;
}

ps. Make sure the control exists, otherwise you will receive an exception.

Another option would be to generate the controls inside the loop, and then generate only the required amount (I would do it that way).

Considering that the PictureBox will be within panel1 :

panel1.Controls.Clear();
for (int i =0; i < 5; i++)
{
    PictureBox pb = new PictureBox();
    pb.Name = "pb" + i;
    pb.Image = null;
    pb.Parent = panel1;
    pb.Show();
}

Edit:

The first option in your code would look like this:

try
{
    sqlcon.Open();
    string sql2 = "SELECT img FROM clienteimg WHERE ((clienteid = '" + cid + "') AND (id = '" + fid + "'))";
    SqlCommand cmd2 = new SqlCommand(sql2, sqlcon);
    SqlDataReader dr2 = cmd2.ExecuteReader();
    dr2.Read();
    byte[] imgLoc = (byte[])dr2[0];
    sqlcon.Close();
    MemoryStream mstream = new MemoryStream(imgLoc);
    ((PictureBox)this.Controls.Find("pb" + fid , true)[0]).Image = Image.FromStream(mstream);
}
...
  

Remembering that the name of your PictureBox should be "pb" + [every fid you will load]

The second option, yes, will have to remove the PictureBox from the screen, because they will not serve anything. The recommendation is that you use a Panel or FlowLayoutPanel to insert all the PictureBox into it, so it's easy to organize, clean and insert.

But with the second option, change the logic you are using, Select would be different, among other things.

    
05.02.2018 / 12:59