Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > SQL query gets random image, but browser always loads same one

Reply
Thread Tools

SQL query gets random image, but browser always loads same one

 
 
Haydnw
Guest
Posts: n/a
 
      08-13-2004
Hi,

I have the code below as code-behind for a page which displays two images.
My problem is with the second bit of code, commented as " 'Portfolio image
section". Basically, the SQL query gets details of a random image from an
access database. I know this works because when I run the query in access,
it picks a different image each time (or close enough). However, when I view
the .aspx page that this code-behind is for, it always picks the same image.
Can anyone please identify why this might be? I've tried deleting browser
cache etc but still no luck. Apologies for dumping huge lots of code in this
message, but I'm guessing you'll need to see it

Thanks very much,
Haydn


Code-behind:
=========


Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.Oledb
Imports System.Configuration

Public Class GetFrontPageInfo : Inherits Page

Public objConn As OledbConnection

'Declare labels for holding information re Newest album
Public lblstrNewestArtist As Label
Public dtmDate As DateTime
Public lblstrNewestDate As Label
Public lnkNewestAlbum As Hyperlink
Public imgNewestImage AS Image

'Declare labels for holding information re Portfolio image
Public lblstrPortfolioArtist As Label
Public lblstrPortfolioDate As Label
Public imgPortfolioImage AS Image

Public Sub Page_Load(Sender As Object, E As EventArgs)

'Newest album section
'====================

'SQL statement
Dim strSQL as String
strSQL= "SELECT tblEvents.strDirectory, tblEvents.strArtist,
tblEvents.strVenue, tblEvents.strTown, tblEvents.dtmDate FROM tblEvents
INNER JOIN sqyMaxDirOnly ON tblEvents.strDirectory =
sqyMaxDirOnly.strNewestDir;"

'Connection object
objConn = New
OledbConnection(ConfigurationSettings.AppSettings( "strConnString"))

'Command object
Dim objNewestCommand As New OledbCommand(strSQL, objConn)

'Data Reader
Dim objNewestDataReader as OledbDataReader

'Open connection and execute command on data reader
objConn.Open()
objNewestDataReader = objNewestCommand.ExecuteReader()

'Set label text to values pulled from database
'Use 'Do While Loop' so that null values are allowed
'(I know there aren't any null values but it gives an error otherwise!)
Do While objNewestDataReader.Read()=True
imgNewestImage.ImageUrl = "images/newest/" &
objNewestDataReader("strDirectory") & ".jpg"
lnkNewestAlbum.NavigateUrl = "html/album.aspx?d=" &
objNewestDataReader("strDirectory")
lblstrNewestArtist.text = objNewestDataReader("strArtist")
'Need to use DateTime object to obtain correct formatting, ie
month and year only
dtmDate = objNewestDataReader("dtmDate")
lblstrPortfolioDate.text = dtmDate.ToString("MMMM yyyy")
Loop

'Close data reader
objNewestDataReader.Close()


'Portfolio image section
'=======================

'SQL statement
strSQL= "SELECT tblEvents.strDirectory, tblEvents.strArtist,
tblEvents.strVenue, tblEvents.strTown, tblEvents.dtmDate,
tblEvents.strArtistWebsite, sqyRandPortImage.strFilename FROM tblEvents
INNER JOIN sqyRandPortImage ON tblEvents.strDirectory =
sqyRandPortImage.strDirectory;"

'Command object
Dim objPortCommand As New OledbCommand(strSQL, objConn)

'Data Reader
Dim objPortDataReader as OledbDataReader

'Execute command on data reader - connection is already open
objPortDataReader = objPortCommand.ExecuteReader()

'Set label text to values pulled from database
'Use 'Do While Loop' so that null values are allowed
'(I know there aren't any null values but it gives an error otherwise!)
Do While objPortDataReader.Read()=True
lblstrPortfolioArtist.text = objPortDataReader("strArtist")
'Need to use DateTime object to obtain correct formatting,
ie month and year only
dtmDate = objPortDataReader("dtmDate")
lblstrNewestDate.text = dtmDate.ToString("MMMM yyyy")
'Set image path
imgPortfolioImage.ImageUrl = "images/albums/" &
objPortDataReader("strDirectory") & "/" & objPortDataReader("strFilename") &
".jpg"
Loop

'Close data reader
objPortDataReader.Close()

'Close connection
objConn.Close()


End Sub


End Class


 
Reply With Quote
 
 
 
 
Peter Morris
Guest
Posts: n/a
 
      08-13-2004
Hi

Why are you writing the DB image to disk? You should have an ASPX page
which returns the image data and sets the content type to image/jpg


--
Pete
====
Audio compression components, DIB graphics controls, FastStrings
http://www.droopyeyes.com

Read or write articles on just about anything
http://www.HowToDoThings.com


 
Reply With Quote
 
 
 
 
Bart
Guest
Posts: n/a
 
      08-13-2004
Forgive me, I couldn't be bothered to read all of your code, however
something that I use that may help. I place a random number in a querystring
in the image URL like this:

Dim randomNumber As New Random
....
imgNewestImage.ImageUrl = "images/newest/" &
objNewestDataReader("strDirectory") & ".jpg?rnd=" & randomNumber.Next(10000)

Try that for each image URL.

David


"Haydnw" <(E-Mail Removed)> wrote in message
news:OZ78$(E-Mail Removed)...
> Hi,
>
> I have the code below as code-behind for a page which displays two images.
> My problem is with the second bit of code, commented as " 'Portfolio image
> section". Basically, the SQL query gets details of a random image from an
> access database. I know this works because when I run the query in access,
> it picks a different image each time (or close enough). However, when I

view
> the .aspx page that this code-behind is for, it always picks the same

image.
> Can anyone please identify why this might be? I've tried deleting browser
> cache etc but still no luck. Apologies for dumping huge lots of code in

this
> message, but I'm guessing you'll need to see it
>
> Thanks very much,
> Haydn
>
>
> Code-behind:
> =========
>
>
> Imports System
> Imports System.Web.UI
> Imports System.Web.UI.WebControls
> Imports System.Data
> Imports System.Data.Oledb
> Imports System.Configuration
>
> Public Class GetFrontPageInfo : Inherits Page
>
> Public objConn As OledbConnection
>
> 'Declare labels for holding information re Newest album
> Public lblstrNewestArtist As Label
> Public dtmDate As DateTime
> Public lblstrNewestDate As Label
> Public lnkNewestAlbum As Hyperlink
> Public imgNewestImage AS Image
>
> 'Declare labels for holding information re Portfolio image
> Public lblstrPortfolioArtist As Label
> Public lblstrPortfolioDate As Label
> Public imgPortfolioImage AS Image
>
> Public Sub Page_Load(Sender As Object, E As EventArgs)
>
> 'Newest album section
> '====================
>
> 'SQL statement
> Dim strSQL as String
> strSQL= "SELECT tblEvents.strDirectory, tblEvents.strArtist,
> tblEvents.strVenue, tblEvents.strTown, tblEvents.dtmDate FROM tblEvents
> INNER JOIN sqyMaxDirOnly ON tblEvents.strDirectory =
> sqyMaxDirOnly.strNewestDir;"
>
> 'Connection object
> objConn = New
> OledbConnection(ConfigurationSettings.AppSettings( "strConnString"))
>
> 'Command object
> Dim objNewestCommand As New OledbCommand(strSQL, objConn)
>
> 'Data Reader
> Dim objNewestDataReader as OledbDataReader
>
> 'Open connection and execute command on data reader
> objConn.Open()
> objNewestDataReader = objNewestCommand.ExecuteReader()
>
> 'Set label text to values pulled from database
> 'Use 'Do While Loop' so that null values are allowed
> '(I know there aren't any null values but it gives an error

otherwise!)
> Do While objNewestDataReader.Read()=True
> imgNewestImage.ImageUrl = "images/newest/" &
> objNewestDataReader("strDirectory") & ".jpg"
> lnkNewestAlbum.NavigateUrl = "html/album.aspx?d=" &
> objNewestDataReader("strDirectory")
> lblstrNewestArtist.text = objNewestDataReader("strArtist")
> 'Need to use DateTime object to obtain correct formatting, ie
> month and year only
> dtmDate = objNewestDataReader("dtmDate")
> lblstrPortfolioDate.text = dtmDate.ToString("MMMM yyyy")
> Loop
>
> 'Close data reader
> objNewestDataReader.Close()
>
>
> 'Portfolio image section
> '=======================
>
> 'SQL statement
> strSQL= "SELECT tblEvents.strDirectory, tblEvents.strArtist,
> tblEvents.strVenue, tblEvents.strTown, tblEvents.dtmDate,
> tblEvents.strArtistWebsite, sqyRandPortImage.strFilename FROM tblEvents
> INNER JOIN sqyRandPortImage ON tblEvents.strDirectory =
> sqyRandPortImage.strDirectory;"
>
> 'Command object
> Dim objPortCommand As New OledbCommand(strSQL, objConn)
>
> 'Data Reader
> Dim objPortDataReader as OledbDataReader
>
> 'Execute command on data reader - connection is already open
> objPortDataReader = objPortCommand.ExecuteReader()
>
> 'Set label text to values pulled from database
> 'Use 'Do While Loop' so that null values are allowed
> '(I know there aren't any null values but it gives an error

otherwise!)
> Do While objPortDataReader.Read()=True
> lblstrPortfolioArtist.text =

objPortDataReader("strArtist")
> 'Need to use DateTime object to obtain correct formatting,
> ie month and year only
> dtmDate = objPortDataReader("dtmDate")
> lblstrNewestDate.text = dtmDate.ToString("MMMM yyyy")
> 'Set image path
> imgPortfolioImage.ImageUrl = "images/albums/" &
> objPortDataReader("strDirectory") & "/" & objPortDataReader("strFilename")

&
> ".jpg"
> Loop
>
> 'Close data reader
> objPortDataReader.Close()
>
> 'Close connection
> objConn.Close()
>
>
> End Sub
>
>
> End Class
>
>



 
Reply With Quote
 
=?Utf-8?B?SGF5ZG53?=
Guest
Posts: n/a
 
      08-13-2004
Hi Pete,

Forgive me, but I don't follow. I don't write the image to disk (do I?) -
this page is for viewing images. I merely get the directory and filename of
an image from the database, concatenate them and use that to set the ImageUrl
property of an <asp:image>. If you could expand on your previous post I would
be very grateful.

Thanks,
Haydn


"Peter Morris" wrote:

> Hi
>
> Why are you writing the DB image to disk? You should have an ASPX page
> which returns the image data and sets the content type to image/jpg
>
>
> --
> Pete
> ====
> Audio compression components, DIB graphics controls, FastStrings
> http://www.droopyeyes.com
>
> Read or write articles on just about anything
> http://www.HowToDoThings.com
>
>
>

 
Reply With Quote
 
Haydnw
Guest
Posts: n/a
 
      08-13-2004
Hi all,

Figured out what makes this go wrong - it's the objConn.Close() on the very
last line. However, if closing the connection here breaks my page, where is
the best place to close it? Or it is just closed automatically after the
page loads? I don't want loads of open connections to the database, do I?!

Cheers,
H


"Haydnw" <(E-Mail Removed)> wrote in message
news:OZ78$(E-Mail Removed)...
> Hi,
>
> I have the code below as code-behind for a page which displays two images.
> My problem is with the second bit of code, commented as " 'Portfolio image
> section". Basically, the SQL query gets details of a random image from an
> access database. I know this works because when I run the query in access,
> it picks a different image each time (or close enough). However, when I

view
> the .aspx page that this code-behind is for, it always picks the same

image.
> Can anyone please identify why this might be? I've tried deleting browser
> cache etc but still no luck. Apologies for dumping huge lots of code in

this
> message, but I'm guessing you'll need to see it
>
> Thanks very much,
> Haydn
>
>
> Code-behind:
> =========
>
>
> Imports System
> Imports System.Web.UI
> Imports System.Web.UI.WebControls
> Imports System.Data
> Imports System.Data.Oledb
> Imports System.Configuration
>
> Public Class GetFrontPageInfo : Inherits Page
>
> Public objConn As OledbConnection
>
> 'Declare labels for holding information re Newest album
> Public lblstrNewestArtist As Label
> Public dtmDate As DateTime
> Public lblstrNewestDate As Label
> Public lnkNewestAlbum As Hyperlink
> Public imgNewestImage AS Image
>
> 'Declare labels for holding information re Portfolio image
> Public lblstrPortfolioArtist As Label
> Public lblstrPortfolioDate As Label
> Public imgPortfolioImage AS Image
>
> Public Sub Page_Load(Sender As Object, E As EventArgs)
>
> 'Newest album section
> '====================
>
> 'SQL statement
> Dim strSQL as String
> strSQL= "SELECT tblEvents.strDirectory, tblEvents.strArtist,
> tblEvents.strVenue, tblEvents.strTown, tblEvents.dtmDate FROM tblEvents
> INNER JOIN sqyMaxDirOnly ON tblEvents.strDirectory =
> sqyMaxDirOnly.strNewestDir;"
>
> 'Connection object
> objConn = New
> OledbConnection(ConfigurationSettings.AppSettings( "strConnString"))
>
> 'Command object
> Dim objNewestCommand As New OledbCommand(strSQL, objConn)
>
> 'Data Reader
> Dim objNewestDataReader as OledbDataReader
>
> 'Open connection and execute command on data reader
> objConn.Open()
> objNewestDataReader = objNewestCommand.ExecuteReader()
>
> 'Set label text to values pulled from database
> 'Use 'Do While Loop' so that null values are allowed
> '(I know there aren't any null values but it gives an error

otherwise!)
> Do While objNewestDataReader.Read()=True
> imgNewestImage.ImageUrl = "images/newest/" &
> objNewestDataReader("strDirectory") & ".jpg"
> lnkNewestAlbum.NavigateUrl = "html/album.aspx?d=" &
> objNewestDataReader("strDirectory")
> lblstrNewestArtist.text = objNewestDataReader("strArtist")
> 'Need to use DateTime object to obtain correct formatting, ie
> month and year only
> dtmDate = objNewestDataReader("dtmDate")
> lblstrPortfolioDate.text = dtmDate.ToString("MMMM yyyy")
> Loop
>
> 'Close data reader
> objNewestDataReader.Close()
>
>
> 'Portfolio image section
> '=======================
>
> 'SQL statement
> strSQL= "SELECT tblEvents.strDirectory, tblEvents.strArtist,
> tblEvents.strVenue, tblEvents.strTown, tblEvents.dtmDate,
> tblEvents.strArtistWebsite, sqyRandPortImage.strFilename FROM tblEvents
> INNER JOIN sqyRandPortImage ON tblEvents.strDirectory =
> sqyRandPortImage.strDirectory;"
>
> 'Command object
> Dim objPortCommand As New OledbCommand(strSQL, objConn)
>
> 'Data Reader
> Dim objPortDataReader as OledbDataReader
>
> 'Execute command on data reader - connection is already open
> objPortDataReader = objPortCommand.ExecuteReader()
>
> 'Set label text to values pulled from database
> 'Use 'Do While Loop' so that null values are allowed
> '(I know there aren't any null values but it gives an error

otherwise!)
> Do While objPortDataReader.Read()=True
> lblstrPortfolioArtist.text =

objPortDataReader("strArtist")
> 'Need to use DateTime object to obtain correct formatting,
> ie month and year only
> dtmDate = objPortDataReader("dtmDate")
> lblstrNewestDate.text = dtmDate.ToString("MMMM yyyy")
> 'Set image path
> imgPortfolioImage.ImageUrl = "images/albums/" &
> objPortDataReader("strDirectory") & "/" & objPortDataReader("strFilename")

&
> ".jpg"
> Loop
>
> 'Close data reader
> objPortDataReader.Close()
>
> 'Close connection
> objConn.Close()
>
>
> End Sub
>
>
> End Class
>
>



 
Reply With Quote
 
=?Utf-8?B?amdyYW50?=
Guest
Posts: n/a
 
      09-25-2004
Do you still need help?

"Haydnw" wrote:

> Hi,
>
> I have the code below as code-behind for a page which displays two images.
> My problem is with the second bit of code, commented as " 'Portfolio image
> section". Basically, the SQL query gets details of a random image from an
> access database. I know this works because when I run the query in access,
> it picks a different image each time (or close enough). However, when I view
> the .aspx page that this code-behind is for, it always picks the same image.
> Can anyone please identify why this might be? I've tried deleting browser
> cache etc but still no luck. Apologies for dumping huge lots of code in this
> message, but I'm guessing you'll need to see it
>
> Thanks very much,
> Haydn
>
>
> Code-behind:
> =========
>
>
> Imports System
> Imports System.Web.UI
> Imports System.Web.UI.WebControls
> Imports System.Data
> Imports System.Data.Oledb
> Imports System.Configuration
>
> Public Class GetFrontPageInfo : Inherits Page
>
> Public objConn As OledbConnection
>
> 'Declare labels for holding information re Newest album
> Public lblstrNewestArtist As Label
> Public dtmDate As DateTime
> Public lblstrNewestDate As Label
> Public lnkNewestAlbum As Hyperlink
> Public imgNewestImage AS Image
>
> 'Declare labels for holding information re Portfolio image
> Public lblstrPortfolioArtist As Label
> Public lblstrPortfolioDate As Label
> Public imgPortfolioImage AS Image
>
> Public Sub Page_Load(Sender As Object, E As EventArgs)
>
> 'Newest album section
> '====================
>
> 'SQL statement
> Dim strSQL as String
> strSQL= "SELECT tblEvents.strDirectory, tblEvents.strArtist,
> tblEvents.strVenue, tblEvents.strTown, tblEvents.dtmDate FROM tblEvents
> INNER JOIN sqyMaxDirOnly ON tblEvents.strDirectory =
> sqyMaxDirOnly.strNewestDir;"
>
> 'Connection object
> objConn = New
> OledbConnection(ConfigurationSettings.AppSettings( "strConnString"))
>
> 'Command object
> Dim objNewestCommand As New OledbCommand(strSQL, objConn)
>
> 'Data Reader
> Dim objNewestDataReader as OledbDataReader
>
> 'Open connection and execute command on data reader
> objConn.Open()
> objNewestDataReader = objNewestCommand.ExecuteReader()
>
> 'Set label text to values pulled from database
> 'Use 'Do While Loop' so that null values are allowed
> '(I know there aren't any null values but it gives an error otherwise!)
> Do While objNewestDataReader.Read()=True
> imgNewestImage.ImageUrl = "images/newest/" &
> objNewestDataReader("strDirectory") & ".jpg"
> lnkNewestAlbum.NavigateUrl = "html/album.aspx?d=" &
> objNewestDataReader("strDirectory")
> lblstrNewestArtist.text = objNewestDataReader("strArtist")
> 'Need to use DateTime object to obtain correct formatting, ie
> month and year only
> dtmDate = objNewestDataReader("dtmDate")
> lblstrPortfolioDate.text = dtmDate.ToString("MMMM yyyy")
> Loop
>
> 'Close data reader
> objNewestDataReader.Close()
>
>
> 'Portfolio image section
> '=======================
>
> 'SQL statement
> strSQL= "SELECT tblEvents.strDirectory, tblEvents.strArtist,
> tblEvents.strVenue, tblEvents.strTown, tblEvents.dtmDate,
> tblEvents.strArtistWebsite, sqyRandPortImage.strFilename FROM tblEvents
> INNER JOIN sqyRandPortImage ON tblEvents.strDirectory =
> sqyRandPortImage.strDirectory;"
>
> 'Command object
> Dim objPortCommand As New OledbCommand(strSQL, objConn)
>
> 'Data Reader
> Dim objPortDataReader as OledbDataReader
>
> 'Execute command on data reader - connection is already open
> objPortDataReader = objPortCommand.ExecuteReader()
>
> 'Set label text to values pulled from database
> 'Use 'Do While Loop' so that null values are allowed
> '(I know there aren't any null values but it gives an error otherwise!)
> Do While objPortDataReader.Read()=True
> lblstrPortfolioArtist.text = objPortDataReader("strArtist")
> 'Need to use DateTime object to obtain correct formatting,
> ie month and year only
> dtmDate = objPortDataReader("dtmDate")
> lblstrNewestDate.text = dtmDate.ToString("MMMM yyyy")
> 'Set image path
> imgPortfolioImage.ImageUrl = "images/albums/" &
> objPortDataReader("strDirectory") & "/" & objPortDataReader("strFilename") &
> ".jpg"
> Loop
>
> 'Close data reader
> objPortDataReader.Close()
>
> 'Close connection
> objConn.Close()
>
>
> End Sub
>
>
> End Class
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
XMLHttpRequest gets data to display in element but data gets appended not replaces previous! libsfan01 Javascript 5 12-20-2006 06:25 AM
Not only the selected HREF gets surrounded, but the whole row gets surrounded Stefan Mueller HTML 5 07-10-2006 11:53 AM
Build dynamic sql query for JSTL <sql:query> Anonymous Java 0 10-13-2005 10:01 PM



Advertisments