How to display multiple images from sql server 2005 into c# at runtime.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajmca
    New Member
    • Mar 2013
    • 1

    How to display multiple images from sql server 2005 into c# at runtime.

    I STORED MULTIPLE IMAGES IN MY SQL SERVER 2005. NOW I WANT TO RETRIEVE PARTICULAR CATEGORY OF IMAGES INTO MULTIPLE PICTUREBOX AT RUNTIME IN C# WINDOWS APPLICATION. i used the following code in my final result form at form_Load using c# windows apps.. But it displays only one picture at run time pls help to me...


    Code:
    
    
    SqlConnection con = new SqlConnection(@"Data Source=ttt-PC;Initial Catalog=Query_Image;Integrated Security=True");
                SqlCommand cmdnew = new SqlCommand("select pic from Image_Category where IMG_Cat='" + searchvalue.ToString() + "'", con);
                con.Open();
              //  SqlDataReader dr = cmdnew.ExecuteReader();
                SqlDataAdapter da = new SqlDataAdapter(cmdnew);
                DataSet ds = new DataSet("Image_Category");
                byte[] mydata = new byte[0];
                da.Fill(ds, "Image_Category");
                DataRow myrow;
                //if (dr.HasRows)
                //{
                //    while (dr.Read())
                //    {
    
                for (int i = 0; i < 2; i++)
    
                // while (i <= dt.Rows.Count)
    
                {
                                       
                    myrow= ds.Tables["Image_Category"].Rows[i];
                    mydata = (byte[])ds.Tables[0].Rows[i]["pic"];
                    str[i]=new MemoryStream(mydata);
                   
                    pbx[i]=new PictureBox();
                     pbx[i].Size = new Size(150, 150);
                     pbx[i].SizeMode = PictureBoxSizeMode.StretchImage;
                    pbx[i].BackgroundImage = Image.FromStream(str[i]);
                     pbx[i].Visible = true;
                     //this.Controls.Add(pbx[i]);
                    }
    PLEASE TELL IS IT ANY WRONG WITH THIS CODE?
    OR SUGGEST NEW CODE TO ME PLEASE..
  • vijay6
    New Member
    • Mar 2010
    • 158

    #2
    Hey rajmca, your code is right but you forgot to use the PictureBox location property. So by default all the PictureBoxes uses same location (One above one). So that only you're seeing only one PictureBox (the PictureBox which created atlast) at runtime. Include these lines and try,

    Code:
    // Above for loop
    
    int x = 10;
    int y = 10;
    
    // Inside for loop
    
    pbx[i].Location = new Point(x, y);
    x += pbx[i].Location.X + pbx[i].Width;

    And one more thing is you've to use Image property not BackgroundImage property in line #31 in your code.

    Code:
    //pbx[i].BackgroundImage = Image.FromStream(str[i]); // Instead of this line use the following line.
    pbx[i].Image = Image.FromStream(str[i]);

    Comment

    • helsywarner
      New Member
      • Apr 2013
      • 1

      #3
      Dim image As Byte() = DirectCast(comm and.ExecuteScal ar(), Byte())
      stream.Write(im age, 0, image.Length)


      you can retreve each image and put it into bitmap.

      How to retrieve an image from sql server database using sql statements , retrieve image from database


      helsy

      Comment

      • MadhaviN
        New Member
        • Aug 2016
        • 1

        #4
        getting error on pbx n str so wht to do...i want to retreive multiple images in picturebox from database when the form is loaded..

        Comment

        Working...