Results 1 to 3 of 3

Thread: how to display multiple images from sql server at runtime.

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    5

    how to display multiple images from sql server at runtime.

    am having a table that contain some images, I want to retrieve the images from the table and display the inside the picture box on my form in vb.net, I have the code that display just one image at a time...

    Code:
    Dim cn As SqlConnection
    
                cn = New SqlConnection
                cn.ConnectionString = "Data Source=ALLAYE\SQLEXPRESS;Initial Catalog=E-commerce;Integrated Security=True"
    
            Dim cmd As New System.Data.SqlClient.SqlCommand("use [E-commerce];SELECT ProductTB.ProductImage  FROM ProductTB WHERE ProductID = 'shoe1'")
            cn.Open()
                cmd.Connection = cn
    
                Dim ImgStream As New IO.MemoryStream(CType(cmd.ExecuteScalar, Byte()))
                cmd.CommandType = CommandType.Text
    
                ProductImagePictureBox.Image = Image.FromStream(ImgStream)
    
                ImgStream.Dispose()
    
                cmd.Connection.Close()
    my question is how can i update this code to be able to display multiple images into my picture box

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to display multiple images from sql server at runtime.

    it'd be easier to display one image per picturebox. just change the sql…

    Code:
    SELECT ProductTB.ProductImage  FROM ProductTB
    You couldn't continue to use ExecuteScalar. Google ExecuteReader which can deal with multiple results

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to display multiple images from sql server at runtime.

    As .paul. suggests, you should use one PictureBox per image. If you add a FlowLayoutPanel to your form, you can then create PictureBoxes at run-time and add them to it and it will handle the layout for you.
    vb.net Code:
    1. 'Clear any existing images if required.
    2. For Each pictureBox In FlowLayoutPanel1.Controls.Cast(Of PictureBox)().ToArray()
    3.     pictureBox.Image.Dispose()
    4.     pictureBox.Dispose()
    5. Next
    6.  
    7. Using connection As New SqlConnection("Data Source=ALLAYE\SQLEXPRESS;Initial Catalog=E-commerce;Integrated Security=True"),
    8.       command As New SqlCommand("SELECT ProductImage FROM ProductTB", connection)
    9.     connection.Open()
    10.  
    11.     Using reader = command.ExecuteReader()
    12.         Do While reader.Read()
    13.             Using stream As New MemoryStream(DirectCast(reader("ProductImage"), Byte()))
    14.                 Dim img = Image.FromStream(stream)
    15.                 Dim pictureBox As New PictureBox With {.SizeMode = PictureBoxSizeMode.AutoSize,
    16.                                                        .Image = img}
    17.  
    18.                 FlowLayoutPanel1.Controls.Add(pictureBox)
    19.             End Using
    20.         Loop
    21.     End Using
    22. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width