views:

155

answers:

3

I was trying to make a program that shows picture in a folder. The path of each picture was stored in the database. My problem was it only show the last images stored in the database and not all the pictures.

code:

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Try
        Dim ctr As Integer = 0
        Dim pic As String
        Dim sqlconn As New SqlConnection("data source=NMPI_2;initial catalog=IPCS; " & _
                             "password=rhyatco; " & _
                             "persist security info=True; " & _
                             "user id= rhyatco;" & _
                             "packet size=4096")
        sqlconn.Open()
        Dim query As String = "Select picture from Bpicture"
        Dim sqlcomm As New SqlCommand(query, sqlconn)
        Dim reader As SqlDataReader
        reader = sqlcomm.ExecuteReader

        While reader.Read
            pic = reader("picture").ToString
            Me.PictureBox1.Image = Image.FromFile(pic)
            Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        End While

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

any help? thanks

+1  A: 

It's always going to show the last image, because you are retrieving the entire table and retrieving all the records with the While loop. The last image will always be the winner.

There are two possible solutions:

One, randomly retrieve one image from the database. Check this stackoverflow thread on how to randomly select a row. The caveat with this solution is you make a call to the database every time.

Two, retrieve all the images from the database, store the data in a collection and randomly select one of the images from the collection. The caveat with this solution is there maybe too many images to store in a collection in memory, in which case you have to go with One.

Chuck Conway
A: 

Well the problem that I see with what you've provided is that you're running the query and iterating through all the rows in the result set every time the timer fires.

I think what you're really looking for is to download the result set once when the form loads, and then to switch which picture you're looking at on the timer.

One way to just rotate through them all over and over again would be to download the list and populate them into a Queue of filenames, and then on the timer just do something like this (sorry, my example is in C#):

string fileName = imageFileNameQueue.Dequeue();
PictureBox1.Image = Image.FromFile(fileName);
imageFileNameQueue.Enqueue (fileName); // Put the file back at the back of the queue
scwagner
A: 

A simple change to your existing solution that will kind of work is to break out of the while loop with some probability. A problem with this solution is that images later in the query result are much less likely to be shown than earlier images.

I haven't done any VB so I'm coding via Google, but you'll need to create an instance of Random somewhere:

Dim rand as new Random()

Then in your while loop, pull off a random number to see if you should stop:

While reader.Read
    ...
    If rand.Next(10) > 8 Then
       Exit While
    End If
End While

EDIT: You should also move the code to set the Image and SizeMode out of the while loop so that they're only set one time, once you've decided on the image.

Dave