Images in ASP

Thursday Jun 10th 1999 by ServerWatch Staff
Share:

Here is a new tutorial on how to include images in ASP.

by Armand Datema

Here is a new tutorial on how to include images in ASP. Without using blobs or binary fields.

First we start of with the basic HTML

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>imagedbase</title>
</head>
<body>
<div align="center"><center>
<table border="0" width="50%" bgcolor="#000000" cellspacing="4" cellpadding="4">
<tr>
<td width="50%" bgcolor="#000000" align="center"><font face="Verdana" color="#FFFFFF">Image
description</font></td>
<td width="50%" bgcolor="#000000" align="center"><font face="Verdana" color="#FFFFFF">Actual
Image</font></td>
</tr>

Now lets create the database connection

set conntemp=server.createobject("adodb.connection")
Set con = Server.CreateObject("ADODB.Connection")

Now lets open the database connection, create the SQL Statement and open the result set,  my ODBC name here is image and the table i am going to query is called image

con.Open "DSN=image;"
Set rs = server.createobject("adodb.recordset")
rs.Open "select * from image", con
do While not rs.EOF %>

The code so far was pretty standard, creating a connection, opening recordset and run the query. Now lets go to te interesting part, how to add the images :)

First we need to add a bit HTML code for the table display

<tr>
<td width="50%" bgcolor="#FFFFFF" align="center"><font face="Verdana">In the next cell you see the

In the next part of the code we write the name of the image as plain text.

<% Response.write(rs("image")) %>

Some extra HTML code for the table display

picture</font></td>
<td width="50%" bgcolor="#FFFFFF" align="center"><font face="Verdana">

Now we add the image, as you can see from the line code <% Response.write(rs("image")) %> below, its actually very easy, just put the response.write ... in the place of where you would normally put the name of the image.  

<img src="<% Response.write(rs("image")) %>.gif" width="100" height="100" ></font></td>
</tr>

Now lets finish the rest of the code and close the HTML tags.

<%
rs.moveNext
loop
%>
</table>
</center></div>
</body>
</html>

This completes yer ASP page, look below for the complete code of imagedbase.asp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>imagedbase</title>
</head>
<body>
<div align="center"><center>
<table border="0" width="50%" bgcolor="#000000" cellspacing="4" cellpadding="4">
<tr>
<td width="50%" bgcolor="#000000" align="center"><font face="Verdana" color="#FFFFFF">Image
description</font></td>
<td width="50%" bgcolor="#000000" align="center"><font face="Verdana" color="#FFFFFF">Actual
Image</font></td>
</tr>
<%
set conntemp=server.createobject("adodb.connection")
Set con = Server.CreateObject("ADODB.Connection")
con.Open "DSN=image;"
Set rs = server.createobject("adodb.recordset")
rs.Open "select * from image", con
do While not rs.EOF %>
<tr>
<td width="50%" bgcolor="#FFFFFF" align="center"><font face="Verdana">In the next cell you see the <% Response.write(rs("image")) %> picture</font></td>
<td width="50%" bgcolor="#FFFFFF" align="center"><font face="Verdana"><img src="<% Response.write(rs("image")) %>.gif" width="100" height="100" ></font></td>
</tr>
<%
rs.moveNext
loop
%>
</table>
</center></div>
</body>
</html>

Download Code
Images in ASP

light.gif (1591 bytes)

Note: Here you will find extra information regarding the article. It could be additions, aprovements, links to other resources or other methods of resolving this problem. If you were looking for the code, go click the diskette to download it.


Back to the Article


Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved