PDA

View Full Version : getting pictures to show from database


gcapp
08-31-2002, 04:43 PM
Can someone out there help me?
I am new to ASP, so I'm not that experienced with it.

I am trying to display an image from a database on my page - one for each day of the year.

I have a database named Pictures, with two fields PictureID and PictureName(which contains the path to the image on my server).

I need a script that will pull image number one and display it for a day, then the next day pull picture number 2 and so on.

I have tried a few scripts but have had no luck.

If anyone knows of a script to do this - I would greatly appreciate it.

Thank you.

Gary

oracleguy
08-31-2002, 09:51 PM
Okay I started writing one and I've run into a few problems but hoepfully others here will be able solve the problems. The problems are:

On the red line, number 42, there is something wrong with my SQL syntax. And the UpdatePicture function runs even if the if the date is the same.

But I have to go and I didn't want my work to go to waste. Otherwise I'd solve all the problems.

Oh and you need to add another table to your database called 'PicturesNext' and make to colums in called 'NextID' and 'CurrentDate'. No primary key.

[code]
<%
' Create and establish data connection
Set DataConn = Server.CreateObject("ADODB.Connection")
DataConn.ConnectionTimeout = 15
DataConn.CommandTimeout = 30

'Access connection code
DataConn.Open "DBQ=" & Server.MapPath("mydatabase.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;", "username", "password"


' Create and link command object to data connection then set attributes and SQL query
Set cmdDC = Server.CreateObject("ADODB.Command")
cmdDC.ActiveConnection = DataConn

' Create recordset and retrieve values using command object
Set rsDC = Server.CreateObject("ADODB.Recordset")

UpdatePicture

rsDC.Close

cmdDC.CommandType = 1
cmdDC.CommandText = "SELECT * FROM PicturesNext;"

' Opening record set with a forward-only cursor (the 0) and in read-only mode (the 1)
rsDC.Open cmdDC, , 0, 1


'Now figure out what is the next picture to load
dim NextPicID,PicturePath,PictureID,ErrorCodes

NextPicID=rsDC("NextID").Value

rsDC.Close

'Now load the appropirate path from the database

cmdDC.CommandText = "SELECT * FROM `Pictures` WHERE `PictureID`='" & NextPicID & "'"

Set rs = Server.CreateObject("ADODB.RecordSet")

rs.Open cmdDC, ,0,1

If not rs.EOF or rs.BOF then
PicturePath=rs("PictureName").Value
PictureID=rs("PictureID").Value
else
ErrorCodes="EOF"
End If

Function UpdatePicture

'rsDC.Close
cmdDC.CommandText="SELECT * FROM PicturesNext"
rsDC.Open cmdDC, , 0, 1

Dim IDs
IDs=rsDC("NextID").Value

If NOT rsDC("CurrentDate").Value=Date then
'The Date is different so it's time to change pictures
cmdDC.CommandText="UPDATE PicturesNext SET NextID='" & IDs+1 & "', CurrentDate='" & Date & "';"
cmdDC.Execute
End If

End Function

%>
<HTML>
<HEAD>
<TITLE>Picture From Database Script</TITLE>
</HEAD>

<BODY>

<p>Today's Picture<BR>
<IMG src="<%=PicturePath%>" ALT="Today's picture">

</BODY>

</HTML>

gcapp
08-31-2002, 10:33 PM
Oracle guy,
Thanks for your input. If you have the time to figure out the code that isn't right - I would appreciate the help. I will try and figure it out, but being that I am a rookie, I probably won't.

Gary