how to display multiple images from sql server at runtime. RRS feed

  • Question

  • 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, I have the code that display just one image at a time...

    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'")        
     cmd.Connection = cn      
    Dim ImgStream As NewIO.MemoryStream(CType(cmd.ExecuteScalar, Byte()))           
     cmd.CommandType = CommandType.Text 
     ProductImagePictureBox.Image =Image.FromStream(ImgStream)    
    my question is how can i update this code to be able to display multiple images into my picture box

    • Edited by Allaye Saturday, March 23, 2019 5:46 PM
    Saturday, March 23, 2019 5:38 PM

All replies

  • Hello,

    Are you indicating one picture box with more than one image at one time rather then rotating say through images and displaying one image at a time? The standard way is to have one image at a time in a single picture box.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Saturday, March 23, 2019 6:07 PM
  • thanks am having more than one image and I want to display each picturebox with different image when the form loads thanks 
    Saturday, March 23, 2019 6:11 PM
  • Hi,
    you can read all pictures and display PictureBox for every picture in LayoutPanel like this demo:

    Imports System.Data.OleDb
    Imports System.IO
    Public Class Form11
      Private pan As New FlowLayoutPanel With {.Dock = DockStyle.Fill}
      Private WithEvents Button2 As New Button With {.Text = "load all photo", .Dock = DockStyle.Top}
      Private Sub Form11_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.AddRange(New Control() {pan, Button2})
      End Sub
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim bytImage() As Byte
          Using cn As New OleDbConnection(My.Settings.cnDatabase1)
            Using cmd As New OleDbCommand("", cn)
              cmd.CommandText = "select * from Tab1"
              Dim rdr = cmd.ExecuteReader
              While rdr.Read
                Dim myms As New IO.MemoryStream
                bytImage = CType(rdr("photo"), Byte())
                myms.Write(bytImage, 0, bytImage.Count)
                Dim pb As New PictureBox With {.Width = 100, .Height = 100, .SizeMode = PictureBoxSizeMode.StretchImage}
                pb.Image = Image.FromStream(myms)
              End While
            End Using
          End Using
          MsgBox("data load source")
        Catch ex As Exception
        End Try
      End Sub
    End Class

    Viele Grüsse / Best Regards
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Sunday, March 24, 2019 4:24 AM